Passed
Push — 1.0.x ( e6e535...5e8624 )
by Koldo
38:50 queued 27:06
created

FeatureRepositoryFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 20
dl 0
loc 44
ccs 20
cts 21
cp 0.9524
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
{
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