Skip to content

Commit

Permalink
Refactor ConnectionFactory to Use Match Expression (#52)
Browse files Browse the repository at this point in the history
* Refactor ConnectionFactory to Use Match Expression

* Update ConnectionFactory.php

* Fix syntax error and align method signature

* Formatting

---------

Co-authored-by: Jonas Staudenmeir <[email protected]>
  • Loading branch information
usermp and staudenmeir authored Nov 19, 2023
1 parent 90cad2e commit fbfb997
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"require": {
"php": "^8.1",
"illuminate/database": "^10.0"
"illuminate/database": "^10.0",
"ext-pdo": "*"
},
"require-dev": {
"orchestra/testbench": "^8.0",
Expand Down
25 changes: 9 additions & 16 deletions src/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,14 @@ protected function createConnection($driver, $connection, $database, $prefix = '
return $resolver($connection, $database, $prefix, $config); // @codeCoverageIgnore
}

switch ($driver) {
case 'mysql':
return new MySqlConnection($connection, $database, $prefix, $config);
case 'pgsql':
return new PostgresConnection($connection, $database, $prefix, $config);
case 'sqlite':
return new SQLiteConnection($connection, $database, $prefix, $config);
case 'sqlsrv':
return new SqlServerConnection($connection, $database, $prefix, $config);
case 'oracle':
return new OracleConnection($connection, $database, $prefix, $config); // @codeCoverageIgnore
case 'singlestore':
return new SingleStoreConnection($connection, $database, $prefix, $config);
}

throw new InvalidArgumentException("Unsupported driver [{$driver}]"); // @codeCoverageIgnore
return match ($driver) {
'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config),
'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
'oracle' => new OracleConnection($connection, $database, $prefix, $config),
'singlestore' => new SingleStoreConnection($connection, $database, $prefix, $config),
default => throw new InvalidArgumentException("Unsupported driver [{$driver}]"),
};
}
}

0 comments on commit fbfb997

Please sign in to comment.