Skip to content

Commit 49c2d0a

Browse files
committed
[Cache][Lock] PdoAdapter/PdoStore minor cleanup
1 parent 73f7693 commit 49c2d0a

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

Store/PdoStore.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function save(Key $key)
9595
try {
9696
$stmt = $conn->prepare($sql);
9797
} 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))) {
9999
$this->createTable();
100100
}
101101
$stmt = $conn->prepare($sql);
@@ -107,12 +107,12 @@ public function save(Key $key)
107107
try {
108108
$stmt->execute();
109109
} 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))) {
111111
$this->createTable();
112112

113113
try {
114114
$stmt->execute();
115-
} catch (\PDOException $e) {
115+
} catch (\PDOException) {
116116
$this->putOffExpiration($key, $this->initialTtl);
117117
}
118118
} else {
@@ -196,11 +196,7 @@ private function getConnection(): \PDO
196196
*/
197197
public function createTable(): void
198198
{
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()) {
204200
'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",
205201
'sqlite' => "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->tokenCol TEXT NOT NULL, $this->expirationCol INTEGER)",
206202
'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
209205
default => throw new \DomainException(sprintf('Creating the lock table is currently not implemented for platform "%s".', $driver)),
210206
};
211207

212-
$conn->exec($sql);
208+
$this->getConnection()->exec($sql);
213209
}
214210

215211
/**
@@ -224,14 +220,7 @@ private function prune(): void
224220

225221
private function getDriver(): string
226222
{
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);
235224
}
236225

237226
/**
@@ -254,15 +243,13 @@ private function isTableMissing(\PDOException $exception): bool
254243
$driver = $this->getDriver();
255244
$code = $exception->getCode();
256245

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+
};
267254
}
268255
}

0 commit comments

Comments
 (0)