• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

craue / CraueFormFlowBundle / 6028840552

30 Aug 2023 06:45PM UTC coverage: 0.716% (-98.8%) from 99.472%
6028840552

push

github

craue
allow deprecation notices for now

699 of 97589 relevant lines covered (0.72%)

4.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.08
/Storage/DoctrineStorage.php
1
<?php
2

3
namespace Craue\FormFlowBundle\Storage;
4

5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Schema\AbstractSchemaManager;
7
use Doctrine\DBAL\Schema\Column;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Types\Type;
10
use Doctrine\DBAL\Types\Types;
11

12
/**
13
 * Stores data in a Doctrine-managed database.
14
 *
15
 * @author Christian Raue <christian.raue@gmail.com>
16
 * @copyright 2011-2023 Christian Raue
17
 * @license http://opensource.org/licenses/mit-license.php MIT License
18
 */
19
class DoctrineStorage implements StorageInterface {
20

21
        const TABLE = 'craue_form_flow_storage';
22
        const KEY_COLUMN = 'key';
23
        const VALUE_COLUMN = 'value';
24

25
        /**
26
         * @var Connection
27
         */
28
        private $conn;
29

30
        /**
31
         * @var StorageKeyGeneratorInterface
32
         */
33
        private $storageKeyGenerator;
34

35
        /**
36
         * @var AbstractSchemaManager
37
         */
38
        private $schemaManager;
39

40
        /**
41
         * @var string
42
         */
43
        private $keyColumn;
44

45
        /**
46
         * @var string
47
         */
48
        private $valueColumn;
49

50
        public function __construct(Connection $conn, StorageKeyGeneratorInterface $storageKeyGenerator) {
51
                $this->conn = $conn;
1,092✔
52
                $this->storageKeyGenerator = $storageKeyGenerator;
1,092✔
53
                // TODO just call `createSchemaManager()` as soon as DBAL >= 3.1 is required
54
                $this->schemaManager = \method_exists($this->conn, 'createSchemaManager') ? $this->conn->createSchemaManager() : $this->conn->getSchemaManager();
1,092✔
55
                $this->keyColumn = $this->conn->quoteIdentifier(self::KEY_COLUMN);
1,092✔
56
                $this->valueColumn = $this->conn->quoteIdentifier(self::VALUE_COLUMN);
1,092✔
57
        }
58

59
        /**
60
         * {@inheritDoc}
61
         */
62
        public function set($key, $value) {
63
                if (!$this->tableExists()) {
987✔
64
                        $this->createTable();
189✔
65
                }
66

67
                if ($this->has($key)) {
987✔
68
                        $this->conn->update(self::TABLE, [
861✔
69
                                $this->valueColumn => serialize($value),
861✔
70
                        ], [
861✔
71
                                $this->keyColumn => $this->generateKey($key),
861✔
72
                        ]);
861✔
73

74
                        return;
861✔
75
                }
76

77
                $this->conn->insert(self::TABLE, [
987✔
78
                        $this->keyColumn => $this->generateKey($key),
987✔
79
                        $this->valueColumn => serialize($value),
987✔
80
                ]);
987✔
81
        }
82

83
        /**
84
         * {@inheritDoc}
85
         */
86
        public function get($key, $default = null) {
87
                if (!$this->tableExists()) {
987✔
88
                        return $default;
84✔
89
                }
90

91
                $rawValue = $this->getRawValueForKey($key);
945✔
92

93
                if ($rawValue === false) {
945✔
94
                        return $default;
819✔
95
                }
96

97
                return unserialize($rawValue);
924✔
98
        }
99

100
        /**
101
         * {@inheritDoc}
102
         */
103
        public function has($key) {
104
                if (!$this->tableExists()) {
1,029✔
105
                        return false;
42✔
106
                }
107

108
                return $this->getRawValueForKey($key) !== false;
987✔
109
        }
110

111
        /**
112
         * {@inheritDoc}
113
         */
114
        public function remove($key) {
115
                if (!$this->tableExists()) {
63✔
116
                        return;
21✔
117
                }
118

119
                $this->conn->delete(self::TABLE, [
42✔
120
                        $this->keyColumn => $this->generateKey($key),
42✔
121
                ]);
42✔
122
        }
123

124
        /**
125
         * Gets stored raw data for the given key.
126
         * @param string $key
127
         * @return string|false Raw data or false, if no data is available.
128
         */
129
        private function getRawValueForKey($key) {
130
                $qb = $this->conn->createQueryBuilder()
987✔
131
                        ->select($this->valueColumn)
987✔
132
                        ->from(self::TABLE)
987✔
133
                        ->where($this->keyColumn . ' = :key')
987✔
134
                        ->setParameter('key', $this->generateKey($key))
987✔
135
                ;
987✔
136

137
                // TODO just call `executeQuery()` as soon as DBAL >= 2.13.1 is required
138
                $result = \method_exists($qb, 'executeQuery') ? $qb->executeQuery() : $qb->execute();
987✔
139

140
                // TODO remove as soon as Doctrine DBAL >= 3.0 is required
141
                if (!\method_exists($result, 'fetchOne')) {
987✔
142
                        return $result->fetchColumn();
×
143
                }
144

145
                return $result->fetchOne();
987✔
146
        }
147

148
        private function tableExists() {
149
                return $this->schemaManager->tablesExist([self::TABLE]);
1,071✔
150
        }
151

152
        private function createTable() {
153
                // TODO remove as soon as Doctrine DBAL >= 3.0 is required
154
                $stringType = defined('Doctrine\DBAL\Types\Types::STRING') ? Types::STRING : Type::STRING;
189✔
155
                $arrayType = defined('Doctrine\DBAL\Types\Types::ARRAY') ? Types::ARRAY : Type::TARRAY;
189✔
156

157
                $table = new Table(self::TABLE, [
189✔
158
                        new Column($this->keyColumn, Type::getType($stringType)),
189✔
159
                        new Column($this->valueColumn, Type::getType($arrayType)),
189✔
160
                ]);
189✔
161

162
                $table->setPrimaryKey([$this->keyColumn]);
189✔
163
                $this->schemaManager->createTable($table);
189✔
164
        }
165

166
        private function generateKey($key) {
167
                return $this->storageKeyGenerator->generate($key);
987✔
168
        }
169

170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc