|
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
|
|
|
{ |
|
17
|
3 |
|
public function __invoke(ContainerInterface $container): FeatureRepository |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ToggleConfig $config */ |
|
20
|
3 |
|
$config = $container->get(ToggleConfig::class); |
|
21
|
|
|
/** @var ?Connection $connection */ |
|
22
|
3 |
|
$connection = $container->get(Connection::class); |
|
23
|
|
|
|
|
24
|
3 |
|
return self::create($config, $connection); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
5 |
|
public static function create(ToggleConfig $config, ?Connection $connection): FeatureRepository |
|
28
|
|
|
{ |
|
29
|
5 |
|
$driver = $config->driver(); |
|
30
|
|
|
|
|
31
|
5 |
|
if (ToggleConfig::DRIVER_IN_MEMORY === $driver) { |
|
32
|
2 |
|
return new InMemoryFeatureRepository(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
if (ToggleConfig::DRIVER_DBAL === $driver) { |
|
36
|
|
|
/** @var Connection $connection */ |
|
37
|
3 |
|
return new DbalFeatureRepository($connection); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
if (ToggleConfig::DRIVER_CHAIN === $driver) { |
|
41
|
1 |
|
$drivers = []; |
|
42
|
1 |
|
foreach ($config->driverOptions() as $driverOption) { |
|
43
|
1 |
|
$drivers[] = self::create(self::getDriverConfig($driverOption, $config), $connection); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return new ChainFeatureRepository(...$drivers); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
throw new InvalidArgumentException('Valid driver required'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
private static function getDriverConfig(string $driverOption, ToggleConfig $config): ToggleConfig |
|
53
|
|
|
{ |
|
54
|
1 |
|
return new ToggleConfig([ |
|
55
|
|
|
'driver' => $driverOption, |
|
56
|
1 |
|
'api_enabled' => $config->apiEnabled(), |
|
57
|
1 |
|
'api_prefix' => $config->apiPrefix(), |
|
58
|
1 |
|
'toggles' => $config->toggles(), |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|