Skip to content

Commit 42ffb90

Browse files
committed
Leverage the match expression
1 parent 92ba953 commit 42ffb90

File tree

2 files changed

+35
-65
lines changed

2 files changed

+35
-65
lines changed

Store/DoctrineDbalStore.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -223,28 +223,17 @@ private function prune(): void
223223
private function getCurrentTimestampStatement(): string
224224
{
225225
$platform = $this->conn->getDatabasePlatform();
226-
switch (true) {
227-
case $platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform:
228-
case $platform instanceof \Doctrine\DBAL\Platforms\MySQL57Platform:
229-
return 'UNIX_TIMESTAMP()';
230-
231-
case $platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform:
232-
return 'strftime(\'%s\',\'now\')';
233-
234-
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform:
235-
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform:
236-
return 'CAST(EXTRACT(epoch FROM NOW()) AS INT)';
237-
238-
case $platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform:
239-
return '(SYSDATE - TO_DATE(\'19700101\',\'yyyymmdd\'))*86400 - TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone), 1, 3))*3600';
240-
241-
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform:
242-
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform:
243-
return 'DATEDIFF(s, \'1970-01-01\', GETUTCDATE())';
244-
245-
default:
246-
return (string) time();
247-
}
226+
return match (true) {
227+
$platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform,
228+
$platform instanceof \Doctrine\DBAL\Platforms\MySQL57Platform => 'UNIX_TIMESTAMP()',
229+
$platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform => 'strftime(\'%s\',\'now\')',
230+
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform,
231+
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform => 'CAST(EXTRACT(epoch FROM NOW()) AS INT)',
232+
$platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform => '(SYSDATE - TO_DATE(\'19700101\',\'yyyymmdd\'))*86400 - TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone), 1, 3))*3600',
233+
$platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform,
234+
$platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform => 'DATEDIFF(s, \'1970-01-01\', GETUTCDATE())',
235+
default => (string) time(),
236+
};
248237
}
249238

250239
/**
@@ -254,15 +243,13 @@ private function platformSupportsTableCreationInTransaction(): bool
254243
{
255244
$platform = $this->conn->getDatabasePlatform();
256245

257-
switch (true) {
258-
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform:
259-
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform:
260-
case $platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform:
261-
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform:
262-
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform:
263-
return true;
264-
default:
265-
return false;
266-
}
246+
return match (true) {
247+
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform,
248+
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform,
249+
$platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform,
250+
$platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform,
251+
$platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform => true,
252+
default => false,
253+
};
267254
}
268255
}

Store/PdoStore.php

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,14 @@ public function createTable(): void
193193
$conn = $this->getConnection();
194194
$driver = $this->getDriver();
195195

196-
switch ($driver) {
197-
case 'mysql':
198-
$sql = "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";
199-
break;
200-
case 'sqlite':
201-
$sql = "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->tokenCol TEXT NOT NULL, $this->expirationCol INTEGER)";
202-
break;
203-
case 'pgsql':
204-
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR(64) NOT NULL, $this->expirationCol INTEGER)";
205-
break;
206-
case 'oci':
207-
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR2(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR2(64) NOT NULL, $this->expirationCol INTEGER)";
208-
break;
209-
case 'sqlsrv':
210-
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR(64) NOT NULL, $this->expirationCol INTEGER)";
211-
break;
212-
default:
213-
throw new \DomainException(sprintf('Creating the lock table is currently not implemented for platform "%s".', $driver));
214-
}
196+
$sql = match ($driver) {
197+
'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",
198+
'sqlite' => "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->tokenCol TEXT NOT NULL, $this->expirationCol INTEGER)",
199+
'pgsql' => "CREATE TABLE $this->table ($this->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR(64) NOT NULL, $this->expirationCol INTEGER)",
200+
'oci' => "CREATE TABLE $this->table ($this->idCol VARCHAR2(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR2(64) NOT NULL, $this->expirationCol INTEGER)",
201+
'sqlsrv' => "CREATE TABLE $this->table ($this->idCol VARCHAR(64) NOT NULL PRIMARY KEY, $this->tokenCol VARCHAR(64) NOT NULL, $this->expirationCol INTEGER)",
202+
default => throw new \DomainException(sprintf('Creating the lock table is currently not implemented for platform "%s".', $driver)),
203+
};
215204

216205
$conn->exec($sql);
217206
}
@@ -243,19 +232,13 @@ private function getDriver(): string
243232
*/
244233
private function getCurrentTimestampStatement(): string
245234
{
246-
switch ($this->getDriver()) {
247-
case 'mysql':
248-
return 'UNIX_TIMESTAMP()';
249-
case 'sqlite':
250-
return 'strftime(\'%s\',\'now\')';
251-
case 'pgsql':
252-
return 'CAST(EXTRACT(epoch FROM NOW()) AS INT)';
253-
case 'oci':
254-
return '(SYSDATE - TO_DATE(\'19700101\',\'yyyymmdd\'))*86400 - TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone), 1, 3))*3600';
255-
case 'sqlsrv':
256-
return 'DATEDIFF(s, \'1970-01-01\', GETUTCDATE())';
257-
default:
258-
return (string) time();
259-
}
235+
return match ($this->getDriver()) {
236+
'mysql' => 'UNIX_TIMESTAMP()',
237+
'sqlite' => 'strftime(\'%s\',\'now\')',
238+
'pgsql' => 'CAST(EXTRACT(epoch FROM NOW()) AS INT)',
239+
'oci' => '(SYSDATE - TO_DATE(\'19700101\',\'yyyymmdd\'))*86400 - TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone), 1, 3))*3600',
240+
'sqlsrv' => 'DATEDIFF(s, \'1970-01-01\', GETUTCDATE())',
241+
default => (string) time(),
242+
};
260243
}
261244
}

0 commit comments

Comments
 (0)