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

craue / CraueFormFlowBundle / 7482490077

11 Jan 2024 12:26AM UTC coverage: 99.482%. Remained the same
7482490077

push

github

web-flow
Merge pull request #422 from franmomu/sf7

Allow Symfony 7

768 of 772 relevant lines covered (99.48%)

937.47 hits per line

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

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

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

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

74
                        return;
1,347✔
75
                }
76

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

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

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

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

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

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

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

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

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

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

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

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

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

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

162
        private function generateKey($key) {
163
                return $this->storageKeyGenerator->generate($key);
1,545✔
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