Passed
Push — 1.0.x ( f3fc24...1b84bc )
by Koldo
02:33
created

FeatureFinderFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 3
rs 10
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\FeatureFinder;
10
use Pheature\Dbal\Toggle\Read\DbalFeatureFactory;
11
use Pheature\Dbal\Toggle\Read\DbalFeatureFinder;
12
use Pheature\InMemory\Toggle\InMemoryConfig;
13
use Pheature\InMemory\Toggle\InMemoryFeatureFactory;
14
use Pheature\InMemory\Toggle\InMemoryFeatureFinder;
15
use Psr\Container\ContainerInterface;
16
17
final class FeatureFinderFactory
18
{
19 3
    public function __invoke(ContainerInterface $container): FeatureFinder
20
    {
21
        /** @var ToggleConfig $config */
22 3
        $config = $container->get(ToggleConfig::class);
23
        /** @var ?Connection $connection */
24 3
        $connection = $container->get(Connection::class);
25
26 3
        return self::create($config, $connection);
27
    }
28
29 5
    public static function create(ToggleConfig $config, ?Connection $connection): FeatureFinder
30
    {
31
32 5
        $driver = $config->driver();
33
34 5
        if ('inmemory' === $driver) {
35 2
            return new InMemoryFeatureFinder(
36 2
                new InMemoryConfig($config->toggles()),
37 2
                new InMemoryFeatureFactory()
38
            );
39
        }
40
41 3
        if ('dbal' === $driver) {
42
            /** @var Connection $connection */
43 2
            return new DbalFeatureFinder($connection, new DbalFeatureFactory());
44
        }
45
46 1
        throw new InvalidArgumentException('Valid driver required');
47
    }
48
}
49