@@ -95,7 +95,7 @@ public function save(Key $key)
95
95
try {
96
96
$ stmt = $ conn ->prepare ($ sql );
97
97
} catch (\PDOException $ e ) {
98
- if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->driver , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
98
+ if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->getDriver () , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
99
99
$ this ->createTable ();
100
100
}
101
101
$ stmt = $ conn ->prepare ($ sql );
@@ -107,12 +107,12 @@ public function save(Key $key)
107
107
try {
108
108
$ stmt ->execute ();
109
109
} catch (\PDOException $ e ) {
110
- if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->driver , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
110
+ if ($ this ->isTableMissing ($ e ) && (!$ conn ->inTransaction () || \in_array ($ this ->getDriver () , ['pgsql ' , 'sqlite ' , 'sqlsrv ' ], true ))) {
111
111
$ this ->createTable ();
112
112
113
113
try {
114
114
$ stmt ->execute ();
115
- } catch (\PDOException $ e ) {
115
+ } catch (\PDOException ) {
116
116
$ this ->putOffExpiration ($ key , $ this ->initialTtl );
117
117
}
118
118
} else {
@@ -196,11 +196,7 @@ private function getConnection(): \PDO
196
196
*/
197
197
public function createTable (): void
198
198
{
199
- // connect if we are not yet
200
- $ conn = $ this ->getConnection ();
201
- $ driver = $ this ->getDriver ();
202
-
203
- $ sql = match ($ driver ) {
199
+ $ sql = match ($ driver = $ this ->getDriver ()) {
204
200
'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 " ,
205
201
'sqlite ' => "CREATE TABLE $ this ->table ( $ this ->idCol TEXT NOT NULL PRIMARY KEY, $ this ->tokenCol TEXT NOT NULL, $ this ->expirationCol INTEGER) " ,
206
202
'pgsql ' => "CREATE TABLE $ this ->table ( $ this ->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $ this ->tokenCol VARCHAR(64) NOT NULL, $ this ->expirationCol INTEGER) " ,
@@ -209,7 +205,7 @@ public function createTable(): void
209
205
default => throw new \DomainException (sprintf ('Creating the lock table is currently not implemented for platform "%s". ' , $ driver )),
210
206
};
211
207
212
- $ conn ->exec ($ sql );
208
+ $ this -> getConnection () ->exec ($ sql );
213
209
}
214
210
215
211
/**
@@ -224,14 +220,7 @@ private function prune(): void
224
220
225
221
private function getDriver (): string
226
222
{
227
- if (isset ($ this ->driver )) {
228
- return $ this ->driver ;
229
- }
230
-
231
- $ conn = $ this ->getConnection ();
232
- $ this ->driver = $ conn ->getAttribute (\PDO ::ATTR_DRIVER_NAME );
233
-
234
- return $ this ->driver ;
223
+ return $ this ->driver ??= $ this ->getConnection ()->getAttribute (\PDO ::ATTR_DRIVER_NAME );
235
224
}
236
225
237
226
/**
@@ -254,15 +243,13 @@ private function isTableMissing(\PDOException $exception): bool
254
243
$ driver = $ this ->getDriver ();
255
244
$ code = $ exception ->getCode ();
256
245
257
- switch (true ) {
258
- case 'pgsql ' === $ driver && '42P01 ' === $ code :
259
- case 'sqlite ' === $ driver && str_contains ($ exception ->getMessage (), 'no such table: ' ):
260
- case 'oci ' === $ driver && 942 === $ code :
261
- case 'sqlsrv ' === $ driver && 208 === $ code :
262
- case 'mysql ' === $ driver && 1146 === $ code :
263
- return true ;
264
- default :
265
- return false ;
266
- }
246
+ return match ($ driver ) {
247
+ 'pgsql ' => '42P01 ' === $ code ,
248
+ 'sqlite ' => str_contains ($ exception ->getMessage (), 'no such table: ' ),
249
+ 'oci ' => 942 === $ code ,
250
+ 'sqlsrv ' => 208 === $ code ,
251
+ 'mysql ' => 1146 === $ code ,
252
+ default => false ,
253
+ };
267
254
}
268
255
}
0 commit comments