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

FeatureRepositoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 10
dl 0
loc 26
ccs 10
cts 11
cp 0.9091
rs 10
c 3
b 0
f 2
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 1
A create() 0 14 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\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