Test Failed
Push — 1.0.x ( d6bdd2...40e958 )
by Koldo
02:49
created

FeatureFinderFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 29
dl 0
loc 62
ccs 16
cts 17
cp 0.9412
rs 10
c 2
b 0
f 1
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 33 5
A getDriverConfig() 0 7 1
A __invoke() 0 16 3
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\Read\ChainFeatureFinder;
10
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory;
11
use Pheature\Core\Toggle\Read\FeatureFinder;
12
use Pheature\Dbal\Toggle\Read\DbalFeatureFactory;
13
use Pheature\Dbal\Toggle\Read\DbalFeatureFinder;
14
use Pheature\InMemory\Toggle\InMemoryConfig;
15
use Pheature\InMemory\Toggle\InMemoryFeatureFactory;
16
use Pheature\InMemory\Toggle\InMemoryFeatureFinder;
17
use Psr\Container\ContainerInterface;
18
19
final class FeatureFinderFactory
20 2
{
21
    public function __invoke(ContainerInterface $container): FeatureFinder
22
    {
23 2
        /** @var ToggleConfig $config */
24
        $config = $container->get(ToggleConfig::class);
25 2
        /** @var ChainToggleStrategyFactory $chainToggleStrategyFactory */
26
        $chainToggleStrategyFactory = $container->get(ChainToggleStrategyFactory::class);
27 2
        $connection = null;
28
        if (
29 1
            ToggleConfig::DRIVER_DBAL === $config->driver()
30 1
            || true === in_array(ToggleConfig::DRIVER_DBAL, $config->driverOptions(), true)
31
        ) {
32
            /** @var ?Connection $connection */
33 1
            $connection = $container->get(Connection::class);
34
        }
35
36 4
        return self::create($config, $chainToggleStrategyFactory, $connection);
37
    }
38
39
    public static function create(
40
        ToggleConfig $config,
41
        ChainToggleStrategyFactory $chainToggleStrategyFactory,
42 4
        ?Connection $connection
43
    ): FeatureFinder {
44 4
45 2
        $driver = $config->driver();
46 2
47 2
        if (ToggleConfig::DRIVER_IN_MEMORY === $driver) {
48 2
            return new InMemoryFeatureFinder(
49
                new InMemoryConfig($config->toggles()),
50
                new InMemoryFeatureFactory(
51
                    $chainToggleStrategyFactory
52
                )
53 2
            );
54
        }
55 2
56
        if (ToggleConfig::DRIVER_DBAL === $driver) {
57
            /** @var Connection $connection */
58
            return new DbalFeatureFinder($connection, new DbalFeatureFactory($chainToggleStrategyFactory));
59
        }
60
61
        if (ToggleConfig::DRIVER_CHAIN === $driver) {
62
            $drivers = [];
63
            foreach ($config->driverOptions() as $driverOption) {
64
                $driverConfig = self::getDriverConfig($driverOption, $config);
65
                $drivers[] = self::create($driverConfig, $chainToggleStrategyFactory, $connection);
66
            }
67
68
            return new ChainFeatureFinder(...$drivers);
69
        }
70
71
        throw new InvalidArgumentException('Valid driver required');
72
    }
73
74
    private static function getDriverConfig(string $driverOption, ToggleConfig $config): ToggleConfig
75
    {
76
        return new ToggleConfig([
77
            'driver' => $driverOption,
78
            'api_enabled' => $config->apiEnabled(),
79
            'api_prefix' => $config->apiPrefix(),
80
            'toggles' => $config->toggles(),
81
        ]);
82
    }
83
}
84