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

FeatureFinderFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 23
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9666
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\Read\ChainToggleStrategyFactory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pheature\Crud\Psr11\Togg...inToggleStrategyFactory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Pheature\Core\Toggle\Read\FeatureFinder;
11
use Pheature\Dbal\Toggle\Read\DbalFeatureFactory;
12
use Pheature\Dbal\Toggle\Read\DbalFeatureFinder;
13
use Pheature\InMemory\Toggle\InMemoryConfig;
14
use Pheature\InMemory\Toggle\InMemoryFeatureFactory;
15
use Pheature\InMemory\Toggle\InMemoryFeatureFinder;
16
use Psr\Container\ContainerInterface;
17
18
final class FeatureFinderFactory
19
{
20 2
    public function __invoke(ContainerInterface $container): FeatureFinder
21
    {
22
        /** @var ToggleConfig $config */
23 2
        $config = $container->get(ToggleConfig::class);
24
        /** @var ChainToggleStrategyFactory $chainToggleStrategyFactory */
25 2
        $chainToggleStrategyFactory = $container->get(ChainToggleStrategyFactory::class);
26
27 2
        if (ToggleConfig::DRIVER_DBAL === $config->driver()) {
28
            /** @var ?Connection $connection */
29 1
            $connection = $container->get(Connection::class);
30 1
            return self::create($config, $chainToggleStrategyFactory, $connection);
31
        }
32
33 1
        return self::create($config, $chainToggleStrategyFactory, null);
34
    }
35
36 4
    public static function create(
37
        ToggleConfig $config,
38
        ChainToggleStrategyFactory $chainToggleStrategyFactory,
39
        ?Connection $connection
40
    ): FeatureFinder {
41
42 4
        $driver = $config->driver();
43
44 4
        if (ToggleConfig::DRIVER_IN_MEMORY === $driver) {
45 2
            return new InMemoryFeatureFinder(
46 2
                new InMemoryConfig($config->toggles()),
47 2
                new InMemoryFeatureFactory(
48 2
                    $chainToggleStrategyFactory
49
                )
50
            );
51
        }
52
53 2
        if (ToggleConfig::DRIVER_DBAL === $driver) {
54
            /** @var Connection $connection */
55 2
            return new DbalFeatureFinder($connection, new DbalFeatureFactory($chainToggleStrategyFactory));
56
        }
57
58
        throw new InvalidArgumentException('Valid driver required');
59
    }
60
}
61