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

craue / CraueFormFlowBundle / 15439473581

04 Jun 2025 10:02AM UTC coverage: 98.725% (-0.8%) from 99.482%
15439473581

push

github

craue
dropped support for Symfony 4.4, 6.3, 7.0, 7.1

697 of 706 relevant lines covered (98.73%)

838.74 hits per line

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

98.0
/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-2024 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,404✔
52
                $this->storageKeyGenerator = $storageKeyGenerator;
1,404✔
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,404✔
55
                $this->keyColumn = $this->conn->quoteIdentifier(self::KEY_COLUMN);
1,404✔
56
                $this->valueColumn = $this->conn->quoteIdentifier(self::VALUE_COLUMN);
1,404✔
57
        }
58

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

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

74
                        return;
1,107✔
75
                }
76

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

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

91
                $rawValue = $this->getRawValueForKey($key);
1,215✔
92

93
                if ($rawValue === false) {
1,215✔
94
                        return $default;
1,053✔
95
                }
96

97
                return unserialize($rawValue);
1,188✔
98
        }
99

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

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

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

119
                $this->conn->delete(self::TABLE, [
54✔
120
                        $this->keyColumn => $this->generateKey($key),
54✔
121
                ]);
54✔
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()
1,269✔
131
                        ->select($this->valueColumn)
1,269✔
132
                        ->from(self::TABLE)
1,269✔
133
                        ->where($this->keyColumn . ' = :key')
1,269✔
134
                        ->setParameter('key', $this->generateKey($key))
1,269✔
135
                ;
1,269✔
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();
1,269✔
139

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

145
                return $result->fetchOne();
1,269✔
146
        }
147

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

152
        private function createTable() {
153
                $table = new Table(self::TABLE, [
243✔
154
                        new Column($this->keyColumn, Type::getType(Types::STRING), ['length' => 255]),
243✔
155
                        new Column($this->valueColumn, Type::getType(Types::TEXT)),
243✔
156
                ]);
243✔
157

158
                $table->setPrimaryKey([$this->keyColumn]);
243✔
159
                $this->schemaManager->createTable($table);
243✔
160
        }
161

162
        private function generateKey($key) {
163
                return $this->storageKeyGenerator->generate($key);
1,269✔
164
        }
165

166
}
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