Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
18 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| DriverFactory | |
90.00% |
18 / 20 |
|
75.00% |
3 / 4 |
10.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createDriver | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| MySQL | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Projom\Storage\Engine\Driver; |
| 6 | |
| 7 | use Projom\Storage\Engine\Driver\Config; |
| 8 | use Projom\Storage\Engine\Driver\Driver; |
| 9 | use Projom\Storage\Engine\Driver\DriverBase; |
| 10 | use Projom\Storage\Engine\Driver\MySQL; |
| 11 | use Projom\Storage\Engine\Driver\Connection\ConnectionFactory; |
| 12 | |
| 13 | class DriverFactory |
| 14 | { |
| 15 | private ConnectionFactory $connectionFactory; |
| 16 | |
| 17 | public function __construct(ConnectionFactory $connectionFactory) |
| 18 | { |
| 19 | $this->connectionFactory = $connectionFactory; |
| 20 | } |
| 21 | |
| 22 | public static function create(ConnectionFactory $connectionFactory): DriverFactory |
| 23 | { |
| 24 | return new DriverFactory($connectionFactory); |
| 25 | } |
| 26 | |
| 27 | public function createDriver(Config $config): DriverBase |
| 28 | { |
| 29 | if (!$config->hasConnections()) |
| 30 | throw new \Exception('No connections found in driver configuration', 400); |
| 31 | |
| 32 | $driver = match ($config->driver) { |
| 33 | Driver::MySQL => $this->MySQL($config), |
| 34 | default => throw new \Exception('Driver is not supported', 400) |
| 35 | }; |
| 36 | |
| 37 | return $driver; |
| 38 | } |
| 39 | |
| 40 | public function MySQL(Config $config): MySQL |
| 41 | { |
| 42 | $mysql = MySQL::create(); |
| 43 | |
| 44 | $connections = $this->connectionFactory->PDOConnections($config->connections); |
| 45 | foreach ($connections as $connection) |
| 46 | $mysql->addConnection($connection); |
| 47 | |
| 48 | $conection = array_shift($connections); |
| 49 | $mysql->changeConnection($conection->name()); |
| 50 | |
| 51 | if ($config->hasLogger()) |
| 52 | $mysql->setLogger($config->logger); |
| 53 | |
| 54 | if ($config->hasOptions()) |
| 55 | $mysql->setOptions($config->options); |
| 56 | |
| 57 | return $mysql; |
| 58 | } |
| 59 | } |