FixtureTargetMapProvisioner   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 63
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A provision() 0 10 1
A delegateLoaderMap() 0 23 2
A delegateTargetMap() 0 24 2
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Service\Provisioner;
10
11
use Auryn\Injector;
12
use Daikon\Boot\Fixture\FixtureLoaderMap;
13
use Daikon\Boot\Fixture\FixtureTarget;
14
use Daikon\Boot\Fixture\FixtureTargetMap;
15
use Daikon\Boot\Service\ServiceDefinitionInterface;
16
use Daikon\Config\ConfigProviderInterface;
17
use Daikon\Dbal\Connector\ConnectorMap;
18
use Daikon\Interop\Assertion;
19
20
final class FixtureTargetMapProvisioner implements ProvisionerInterface
21
{
22
    public function provision(
23
        Injector $injector,
24
        ConfigProviderInterface $configProvider,
25
        ServiceDefinitionInterface $serviceDefinition
26
    ): void {
27
        $loaderConfigs = (array)$configProvider->get('fixtures.fixture_loaders', []);
28
        $targetConfigs = (array)$configProvider->get('fixtures.fixture_targets', []);
29
30
        $this->delegateLoaderMap($injector, $loaderConfigs);
31
        $this->delegateTargetMap($injector, $targetConfigs);
32
    }
33
34
    private function delegateLoaderMap(Injector $injector, array $loaderConfigs): void
35
    {
36
        $factory = function (ConnectorMap $connectorMap) use (
37
            $injector,
38
            $loaderConfigs
39
        ): FixtureLoaderMap {
40
            $loaders = [];
41
            foreach ($loaderConfigs as $loaderKey => $loaderConfig) {
42
                Assertion::keyNotExists($loaders, $loaderKey, "Fixture loader '$loaderKey' is already defined.");
43
                $loaders[$loaderKey] = $injector->make(
44
                    $loaderConfig['class'],
45
                    [
46
                        ':connector' => $connectorMap->get($loaderConfig['connector']),
47
                        ':settings' => $loaderConfig['settings'] ?? []
48
                    ]
49
                );
50
            }
51
            return new FixtureLoaderMap($loaders);
52
        };
53
54
        $injector
55
            ->delegate(FixtureLoaderMap::class, $factory)
56
            ->share(FixtureLoaderMap::class);
57
    }
58
59
    private function delegateTargetMap(Injector $injector, array $targetConfigs): void
60
    {
61
        $factory = function (FixtureLoaderMap $loaderMap) use (
62
            $injector,
63
            $targetConfigs
64
        ): FixtureTargetMap {
65
            $targets = [];
66
            foreach ($targetConfigs as $targetKey => $targetConfig) {
67
                Assertion::keyNotExists($targets, $targetKey, "Fixture target '$targetKey' is already defined.");
68
                $targets[$targetKey] = $injector->make(
69
                    FixtureTarget::class,
70
                    [
71
                        ':key' => $targetKey,
72
                        ':enabled' => $targetConfig['enabled'],
73
                        ':fixtureLoader' => $loaderMap->get($targetConfig['fixture_loader'])
74
                    ]
75
                );
76
            }
77
            return new FixtureTargetMap($targets);
78
        };
79
80
        $injector
81
            ->delegate(FixtureTargetMap::class, $factory)
82
            ->share(FixtureTargetMap::class);
83
    }
84
}
85