Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| DSN | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 | |
100.00% |
1 / 1 |
| MySQL | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Projom\Storage\Engine\Driver\Connection; |
| 6 | |
| 7 | use Projom\Storage\Engine\Driver\Driver; |
| 8 | use Projom\Storage\Engine\Driver\Connection\Config; |
| 9 | |
| 10 | class DSN |
| 11 | { |
| 12 | public static function MySQL(Config $config): string |
| 13 | { |
| 14 | if ($config->dsn) |
| 15 | return $config->dsn; |
| 16 | |
| 17 | if (!$host = $config->host) |
| 18 | throw new \Exception('Config is missing host', 400); |
| 19 | if (!$port = $config->port) |
| 20 | throw new \Exception('Config is missing port', 400); |
| 21 | if (!$database = $config->database) |
| 22 | throw new \Exception('Config is missing database', 400); |
| 23 | |
| 24 | $parts = [ |
| 25 | "host=$host", |
| 26 | "port=$port", |
| 27 | "dbname=$database", |
| 28 | ]; |
| 29 | |
| 30 | if ($charset = $config->charset) |
| 31 | $parts[] = "charset=$charset"; |
| 32 | |
| 33 | if ($collation = $config->collation) |
| 34 | $parts[] = "collation=$collation"; |
| 35 | |
| 36 | $driver = Driver::MySQL->value; |
| 37 | |
| 38 | return "$driver:" . implode(';', $parts); |
| 39 | } |
| 40 | } |