Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| Engine | |
100.00% |
26 / 26 |
|
100.00% |
8 / 8 |
13 | |
100.00% |
1 / 1 |
| start | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| clear | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| dispatch | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| useDriver | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| loadDriver | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| setDriver | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setDriverFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| driver | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Projom\Storage; |
| 6 | |
| 7 | use Projom\Storage\Query\Action; |
| 8 | use Projom\Storage\Engine\Driver\DriverBase; |
| 9 | use Projom\Storage\Engine\Driver\Config; |
| 10 | use Projom\Storage\Engine\Driver\Driver; |
| 11 | use Projom\Storage\Engine\Driver\DriverFactory; |
| 12 | use Projom\Storage\Engine\Driver\Connection\ConnectionFactory; |
| 13 | |
| 14 | class Engine |
| 15 | { |
| 16 | protected static array $drivers = []; |
| 17 | protected static null|Driver $currentDriver = null; |
| 18 | protected static null|DriverFactory $driverFactory = null; |
| 19 | |
| 20 | public static function start(): void |
| 21 | { |
| 22 | $connectionFactory = ConnectionFactory::create(); |
| 23 | $driverFactory = DriverFactory::create($connectionFactory); |
| 24 | static::setDriverFactory($driverFactory); |
| 25 | } |
| 26 | |
| 27 | public static function clear(): void |
| 28 | { |
| 29 | static::$drivers = []; |
| 30 | static::$currentDriver = null; |
| 31 | static::$driverFactory = null; |
| 32 | } |
| 33 | |
| 34 | public static function dispatch(Action $action, null|Driver $driver = null, mixed $args = null): mixed |
| 35 | { |
| 36 | if ($driver !== null) |
| 37 | if (static::$currentDriver !== $driver) |
| 38 | static::useDriver($driver); |
| 39 | |
| 40 | $driver = static::driver(); |
| 41 | return $driver->dispatch($action, $args); |
| 42 | } |
| 43 | |
| 44 | public static function useDriver(Driver $driver): void |
| 45 | { |
| 46 | if (!array_key_exists($driver->value, static::$drivers)) |
| 47 | throw new \Exception("Driver not loaded", 400); |
| 48 | static::$currentDriver = $driver; |
| 49 | } |
| 50 | |
| 51 | public static function loadDriver(array $config): void |
| 52 | { |
| 53 | if (static::$driverFactory === null) |
| 54 | throw new \Exception("Driver factory not set", 400); |
| 55 | |
| 56 | $config = new Config($config); |
| 57 | $engineDriver = static::$driverFactory->createDriver($config); |
| 58 | static::setDriver($engineDriver, $config->driver); |
| 59 | } |
| 60 | |
| 61 | public static function setDriver(DriverBase $engineDriver, Driver $driver): void |
| 62 | { |
| 63 | static::$drivers[$driver->value] = $engineDriver; |
| 64 | static::$currentDriver = $driver; |
| 65 | } |
| 66 | |
| 67 | public static function setDriverFactory(DriverFactory $driverFactory): void |
| 68 | { |
| 69 | static::$driverFactory = $driverFactory; |
| 70 | } |
| 71 | |
| 72 | private static function driver(): DriverBase |
| 73 | { |
| 74 | $driver = static::$drivers[static::$currentDriver?->value] ?? null; |
| 75 | if ($driver === null) |
| 76 | throw new \Exception("Engine driver not set", 400); |
| 77 | return $driver; |
| 78 | } |
| 79 | } |