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

FeatureRepositoryFactory::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 3
b 0
f 2
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