Passed
Push — 1.0.x ( 4c6986...086a36 )
by Koldo
10:37
created

FeatureRepositoryFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 3.0261
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\FeatureRepository;
10
use Pheature\Dbal\Toggle\Write\DbalFeatureRepository;
11
use Pheature\InMemory\Toggle\InMemoryFeatureRepository;
12
use Psr\Container\ContainerInterface;
13
14
final class FeatureRepositoryFactory
15
{
16 2
    public function __invoke(ContainerInterface $container): FeatureRepository
17
    {
18
        /** @var ToggleConfig $config */
19 2
        $config = $container->get(ToggleConfig::class);
20
        /** @var ?Connection $connection */
21 2
        $connection = $container->get(Connection::class);
22
23 2
        return self::create($config, $connection);
24
    }
25
26 4
    public static function create(ToggleConfig $config, ?Connection $connection): FeatureRepository
27
    {
28 4
        $driver = $config->driver();
29
30 4
        if (ToggleConfig::DRIVER_IN_MEMORY === $driver) {
31 2
            return new InMemoryFeatureRepository();
32
        }
33
34 2
        if (ToggleConfig::DRIVER_DBAL === $driver) {
35
            /** @var Connection $connection */
36 2
            return new DbalFeatureRepository($connection);
37
        }
38
39
        throw new InvalidArgumentException('Valid driver required');
40
    }
41
}
42