@@ -92,7 +92,7 @@ public function save(Key $key): void
92
92
try {
93
93
$ stmt = $ conn ->prepare ($ sql );
94
94
} catch (\PDOException $ e ) {
95
- if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->driver , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
95
+ if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->getDriver () , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
96
96
$ this ->createTable ();
97
97
}
98
98
$ stmt = $ conn ->prepare ($ sql );
@@ -104,12 +104,12 @@ public function save(Key $key): void
104
104
try {
105
105
$ stmt ->execute ();
106
106
} catch (\PDOException $ e ) {
107
- if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->driver , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
107
+ if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->getDriver () , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
108
108
$ this ->createTable ();
109
109
110
110
try {
111
111
$ stmt ->execute ();
112
- } catch (\PDOException $ e ) {
112
+ } catch (\PDOException ) {
113
113
$ this ->putOffExpiration ($ key , $ this ->initialTtl );
114
114
}
115
115
} else {
@@ -187,11 +187,7 @@ private function getConnection(): \PDO
187
187
*/
188
188
public function createTable (): void
189
189
{
190
- // connect if we are not yet
191
- $ conn = $ this ->getConnection ();
192
- $ driver = $ this ->getDriver ();
193
-
194
- $ sql = match ($ driver ) {
190
+ $ sql = match ($ driver = $ this ->getDriver ()) {
195
191
'mysql ' => "CREATE TABLE $ this ->table ( $ this ->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $ this ->tokenCol VARCHAR(44) NOT NULL, $ this ->expirationCol INTEGER UNSIGNED NOT NULL) COLLATE utf8mb4_bin, ENGINE = InnoDB " ,
196
192
'sqlite ' => "CREATE TABLE $ this ->table ( $ this ->idCol TEXT NOT NULL PRIMARY KEY, $ this ->tokenCol TEXT NOT NULL, $ this ->expirationCol INTEGER) " ,
197
193
'pgsql ' => "CREATE TABLE $ this ->table ( $ this ->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $ this ->tokenCol VARCHAR(64) NOT NULL, $ this ->expirationCol INTEGER) " ,
@@ -200,7 +196,7 @@ public function createTable(): void
200
196
default => throw new \DomainException (sprintf ('Creating the lock table is currently not implemented for platform "%s". ' , $ driver )),
201
197
};
202
198
203
- $ conn ->exec ($ sql );
199
+ $ this -> getConnection () ->exec ($ sql );
204
200
}
205
201
206
202
/**
@@ -215,14 +211,7 @@ private function prune(): void
215
211
216
212
private function getDriver (): string
217
213
{
218
- if (isset ($ this ->driver )) {
219
- return $ this ->driver ;
220
- }
221
-
222
- $ conn = $ this ->getConnection ();
223
- $ this ->driver = $ conn ->getAttribute (\PDO ::ATTR_DRIVER_NAME );
224
-
225
- return $ this ->driver ;
214
+ return $ this ->driver ??= $ this ->getConnection ()->getAttribute (\PDO ::ATTR_DRIVER_NAME );
226
215
}
227
216
228
217
/**
@@ -245,15 +234,13 @@ private function isTableMissing(\PDOException $exception): bool
245
234
$ driver = $ this ->getDriver ();
246
235
$ code = $ exception ->getCode ();
247
236
248
- switch (true ) {
249
- case 'pgsql ' === $ driver && '42P01 ' === $ code :
250
- case 'sqlite ' === $ driver && str_contains ($ exception ->getMessage (), 'no such table: ' ):
251
- case 'oci ' === $ driver && 942 === $ code :
252
- case 'sqlsrv ' === $ driver && 208 === $ code :
253
- case 'mysql ' === $ driver && 1146 === $ code :
254
- return true ;
255
- default :
256
- return false ;
257
- }
237
+ return match ($ driver ) {
238
+ 'pgsql ' => '42P01 ' === $ code ,
239
+ 'sqlite ' => str_contains ($ exception ->getMessage (), 'no such table: ' ),
240
+ 'oci ' => 942 === $ code ,
241
+ 'sqlsrv ' => 208 === $ code ,
242
+ 'mysql ' => 1146 === $ code ,
243
+ default => false ,
244
+ };
258
245
}
259
246
}
0 commit comments