Test Failed
Push — 1.0.x ( 40e958...3e316d )
by Koldo
03:04
created

FeatureRepositoryFactory::getDriverConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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