Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ConnectionFactory | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
7.05 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| create | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
6.04 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\DataStorage\Database\Connection |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\DataStorage\Database\Connection; |
| 16 | |
| 17 | use phpOMS\DataStorage\Database\DatabaseType; |
| 18 | |
| 19 | /** |
| 20 | * Database connection factory. |
| 21 | * |
| 22 | * @package phpOMS\DataStorage\Database\Connection |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class ConnectionFactory |
| 28 | { |
| 29 | /** |
| 30 | * Constructor. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @codeCoverageIgnore |
| 34 | */ |
| 35 | private function __construct() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create database connection. |
| 41 | * |
| 42 | * Overwrites current connection if existing |
| 43 | * |
| 44 | * @param array{db:string, host?:string, port?:int, login?:string, password?:string, database:string} $dbdata the basic database information for establishing a connection |
| 45 | * |
| 46 | * @return ConnectionAbstract |
| 47 | * |
| 48 | * @throws \InvalidArgumentException throws this exception if the database is not supported |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | public static function create(array $dbdata) : ConnectionAbstract |
| 53 | { |
| 54 | switch ($dbdata['db']) { |
| 55 | case DatabaseType::MYSQL: |
| 56 | return new MysqlConnection($dbdata); |
| 57 | case DatabaseType::PGSQL: |
| 58 | return new PostgresConnection($dbdata); |
| 59 | case DatabaseType::SQLSRV: |
| 60 | return new SqlServerConnection($dbdata); |
| 61 | case DatabaseType::SQLITE: |
| 62 | return new SQLiteConnection($dbdata); |
| 63 | default: |
| 64 | throw new \InvalidArgumentException('Database "' . $dbdata['db'] . '" is not supported.'); |
| 65 | } |
| 66 | } |
| 67 | } |