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

FeatureRepositoryFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 20
dl 0
loc 44
ccs 10
cts 11
cp 0.9091
rs 10
c 3
b 0
f 2
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 23 5
A __invoke() 0 8 1
A getDriverConfig() 0 7 1
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