55use Doctrine \DBAL \Connection ;
66use Doctrine \DBAL \Schema \AbstractSchemaManager ;
77use Doctrine \DBAL \Schema \Column ;
8+ use Doctrine \DBAL \Schema \PrimaryKeyConstraint ;
89use Doctrine \DBAL \Schema \Table ;
910use Doctrine \DBAL \Types \Type ;
1011use Doctrine \DBAL \Types \Types ;
@@ -50,10 +51,19 @@ class DoctrineStorage implements StorageInterface {
5051 public function __construct (Connection $ conn , StorageKeyGeneratorInterface $ storageKeyGenerator ) {
5152 $ this ->conn = $ conn ;
5253 $ this ->storageKeyGenerator = $ storageKeyGenerator ;
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 ();
55- $ this ->keyColumn = $ this ->conn ->quoteIdentifier (self ::KEY_COLUMN );
56- $ this ->valueColumn = $ this ->conn ->quoteIdentifier (self ::VALUE_COLUMN );
54+ $ this ->schemaManager = $ this ->conn ->createSchemaManager ();
55+
56+ // BC for doctrine/dbal < 4
57+ /* @phpstan-ignore function.alreadyNarrowedType */
58+ if (method_exists ($ this ->conn , 'quoteSingleIdentifier ' )) {
59+ $ this ->keyColumn = $ this ->conn ->quoteSingleIdentifier (self ::KEY_COLUMN );
60+ $ this ->valueColumn = $ this ->conn ->quoteSingleIdentifier (self ::VALUE_COLUMN );
61+ } else {
62+ /* @phpstan-ignore method.deprecated */
63+ $ this ->keyColumn = $ this ->conn ->quoteIdentifier (self ::KEY_COLUMN );
64+ /* @phpstan-ignore method.deprecated */
65+ $ this ->valueColumn = $ this ->conn ->quoteIdentifier (self ::VALUE_COLUMN );
66+ }
5767 }
5868
5969 /**
@@ -134,15 +144,7 @@ private function getRawValueForKey($key) {
134144 ->setParameter ('key ' , $ this ->generateKey ($ key ))
135145 ;
136146
137- // TODO just call `executeQuery()` as soon as DBAL >= 2.13.1 is required
138- $ result = \method_exists ($ qb , 'executeQuery ' ) ? $ qb ->executeQuery () : $ qb ->execute ();
139-
140- // TODO remove as soon as Doctrine DBAL >= 3.0 is required
141- if (!\method_exists ($ result , 'fetchOne ' )) {
142- return $ result ->fetchColumn ();
143- }
144-
145- return $ result ->fetchOne ();
147+ return $ qb ->executeQuery ()->fetchOne ();
146148 }
147149
148150 private function tableExists () {
@@ -155,12 +157,23 @@ private function createTable() {
155157 new Column ($ this ->valueColumn , Type::getType (Types::TEXT )),
156158 ]);
157159
158- $ table ->setPrimaryKey ([$ this ->keyColumn ]);
160+ // BC for doctrine/dbal < 4
161+ /* @phpstan-ignore function.alreadyNarrowedType */
162+ if (method_exists ($ table , 'addPrimaryKeyConstraint ' )) {
163+ $ table ->addPrimaryKeyConstraint (
164+ PrimaryKeyConstraint::editor ()
165+ ->setUnquotedColumnNames ($ this ->keyColumn )
166+ ->create ()
167+ );
168+ } else {
169+ /* @phpstan-ignore method.deprecated */
170+ $ table ->setPrimaryKey ([$ this ->keyColumn ]);
171+ }
172+
159173 $ this ->schemaManager ->createTable ($ table );
160174 }
161175
162176 private function generateKey ($ key ) {
163177 return $ this ->storageKeyGenerator ->generate ($ key );
164178 }
165-
166179}
0 commit comments