1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pheature\Crud\Psr11\Toggle; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Pheature\Core\Toggle\Write\ChainFeatureRepository; |
10
|
|
|
use Pheature\Core\Toggle\Write\FeatureRepository; |
11
|
|
|
use Pheature\Dbal\Toggle\Write\DbalFeatureRepository; |
12
|
|
|
use Pheature\InMemory\Toggle\InMemoryFeatureRepository; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
final class FeatureRepositoryFactory |
16
|
2 |
|
{ |
17
|
|
|
public function __invoke(ContainerInterface $container): FeatureRepository |
18
|
|
|
{ |
19
|
2 |
|
/** @var ToggleConfig $config */ |
20
|
|
|
$config = $container->get(ToggleConfig::class); |
21
|
2 |
|
/** @var ?Connection $connection */ |
22
|
|
|
$connection = $container->get(Connection::class); |
23
|
2 |
|
|
24
|
|
|
return self::create($config, $connection); |
25
|
|
|
} |
26
|
4 |
|
|
27
|
|
|
public static function create(ToggleConfig $config, ?Connection $connection): FeatureRepository |
28
|
4 |
|
{ |
29
|
|
|
$driver = $config->driver(); |
30
|
4 |
|
|
31
|
2 |
|
if (ToggleConfig::DRIVER_IN_MEMORY === $driver) { |
32
|
|
|
return new InMemoryFeatureRepository(); |
33
|
|
|
} |
34
|
2 |
|
|
35
|
|
|
if (ToggleConfig::DRIVER_DBAL === $driver) { |
36
|
2 |
|
/** @var Connection $connection */ |
37
|
|
|
return new DbalFeatureRepository($connection); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (ToggleConfig::DRIVER_CHAIN === $driver) { |
41
|
|
|
$drivers = []; |
42
|
|
|
foreach ($config->driverOptions() as $driverOption) { |
43
|
|
|
$drivers[] = self::create(self::getDriverConfig($driverOption, $config), $connection); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new ChainFeatureRepository(...$drivers); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new InvalidArgumentException('Valid driver required'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private static function getDriverConfig(string $driverOption, ToggleConfig $config): ToggleConfig |
53
|
|
|
{ |
54
|
|
|
return new ToggleConfig([ |
55
|
|
|
'driver' => $driverOption, |
56
|
|
|
'api_enabled' => $config->apiEnabled(), |
57
|
|
|
'api_prefix' => $config->apiPrefix(), |
58
|
|
|
'toggles' => $config->toggles(), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|