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

craue / CraueFormFlowBundle / 6035545729

31 Aug 2023 09:27AM UTC coverage: 99.15% (-0.3%) from 99.483%
6035545729

push

github

craue
try using `Types::TEXT` for the value column

2 of 2 new or added lines in 1 file covered. (100.0%)

700 of 706 relevant lines covered (99.15%)

464.21 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-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;
777✔
52
                $this->storageKeyGenerator = $storageKeyGenerator;
777✔
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();
777✔
55
                $this->keyColumn = $this->conn->quoteIdentifier(self::KEY_COLUMN);
777✔
56
                $this->valueColumn = $this->conn->quoteIdentifier(self::VALUE_COLUMN);
777✔
57
        }
58

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

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

74
                        return;
612✔
75
                }
76

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc