forked from craue/CraueFormFlowBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoctrineStorage.php
More file actions
179 lines (149 loc) · 4.22 KB
/
Copy pathDoctrineStorage.php
File metadata and controls
179 lines (149 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Craue\FormFlowBundle\Storage;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
/**
* Stores data in a Doctrine-managed database.
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2025 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class DoctrineStorage implements StorageInterface {
const TABLE = 'craue_form_flow_storage';
const KEY_COLUMN = 'key';
const VALUE_COLUMN = 'value';
/**
* @var Connection
*/
private $conn;
/**
* @var StorageKeyGeneratorInterface
*/
private $storageKeyGenerator;
/**
* @var AbstractSchemaManager
*/
private $schemaManager;
/**
* @var string
*/
private $keyColumn;
/**
* @var string
*/
private $valueColumn;
public function __construct(Connection $conn, StorageKeyGeneratorInterface $storageKeyGenerator) {
$this->conn = $conn;
$this->storageKeyGenerator = $storageKeyGenerator;
$this->schemaManager = $this->conn->createSchemaManager();
// BC for doctrine/dbal < 4
/* @phpstan-ignore function.alreadyNarrowedType */
if(method_exists($this->conn, 'quoteSingleIdentifier')) {
$this->keyColumn = $this->conn->quoteSingleIdentifier(self::KEY_COLUMN);
$this->valueColumn = $this->conn->quoteSingleIdentifier(self::VALUE_COLUMN);
} else {
/* @phpstan-ignore method.deprecated */
$this->keyColumn = $this->conn->quoteIdentifier(self::KEY_COLUMN);
/* @phpstan-ignore method.deprecated */
$this->valueColumn = $this->conn->quoteIdentifier(self::VALUE_COLUMN);
}
}
/**
* {@inheritDoc}
*/
public function set($key, $value) {
if (!$this->tableExists()) {
$this->createTable();
}
if ($this->has($key)) {
$this->conn->update(self::TABLE, [
$this->valueColumn => serialize($value),
], [
$this->keyColumn => $this->generateKey($key),
]);
return;
}
$this->conn->insert(self::TABLE, [
$this->keyColumn => $this->generateKey($key),
$this->valueColumn => serialize($value),
]);
}
/**
* {@inheritDoc}
*/
public function get($key, $default = null) {
if (!$this->tableExists()) {
return $default;
}
$rawValue = $this->getRawValueForKey($key);
if ($rawValue === false) {
return $default;
}
return unserialize($rawValue);
}
/**
* {@inheritDoc}
*/
public function has($key) {
if (!$this->tableExists()) {
return false;
}
return $this->getRawValueForKey($key) !== false;
}
/**
* {@inheritDoc}
*/
public function remove($key) {
if (!$this->tableExists()) {
return;
}
$this->conn->delete(self::TABLE, [
$this->keyColumn => $this->generateKey($key),
]);
}
/**
* Gets stored raw data for the given key.
* @param string $key
* @return string|false Raw data or false, if no data is available.
*/
private function getRawValueForKey($key) {
$qb = $this->conn->createQueryBuilder()
->select($this->valueColumn)
->from(self::TABLE)
->where($this->keyColumn . ' = :key')
->setParameter('key', $this->generateKey($key))
;
return $qb->executeQuery()->fetchOne();
}
private function tableExists() {
return $this->schemaManager->tablesExist([self::TABLE]);
}
private function createTable() {
$table = new Table(self::TABLE, [
new Column($this->keyColumn, Type::getType(Types::STRING), ['length' => 255]),
new Column($this->valueColumn, Type::getType(Types::TEXT)),
]);
// BC for doctrine/dbal < 4
/* @phpstan-ignore function.alreadyNarrowedType */
if (method_exists($table, 'addPrimaryKeyConstraint')) {
$table->addPrimaryKeyConstraint(
PrimaryKeyConstraint::editor()
->setUnquotedColumnNames($this->keyColumn)
->create()
);
} else {
/* @phpstan-ignore method.deprecated */
$table->setPrimaryKey([$this->keyColumn]);
}
$this->schemaManager->createTable($table);
}
private function generateKey($key) {
return $this->storageKeyGenerator->generate($key);
}
}