BuildRegulatorStackPass   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 5
dl 0
loc 71
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 24 3
A searchServices() 0 17 4
A inject() 0 22 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\HomeostasisBundle\DependencyInjection\Compiler;
5
6
use Innmind\HomeostasisBundle\Exception\{
7
    MissingAliasAttribute,
8
    MissingRegulatorArgument
9
};
10
use Innmind\Homeostasis\Regulator;
11
use Symfony\Component\DependencyInjection\{
12
    Compiler\CompilerPassInterface,
13
    ContainerBuilder,
14
    Definition,
15
    Reference
16
};
17
18
final class BuildRegulatorStackPass implements CompilerPassInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 10
    public function process(ContainerBuilder $container)
24
    {
25 10
        $stack = $container->getParameter('innmind.homeostasis.regulator_stack');
26 10
        $services = $this->searchServices($container);
27
28 8
        $container->setAlias(
29 8
            'innmind.homeostasis.regulator',
30 8
            $services[$stack[0]]
31
        );
32
33 8
        if (count($stack) === 1) {
34 2
            return;
35
        }
36
37 6
        for ($i = 0, $count = count($stack) - 1; $i < $count; $i++) {
38 6
            $alias = $stack[$i];
39 6
            $next = $stack[$i + 1];
40
41 6
            $this->inject(
42 6
                $container->getDefinition($services[$alias]),
43 6
                $services[$next]
44
            );
45
        }
46 4
    }
47
48 10
    private function searchServices(ContainerBuilder $container): array
49
    {
50 10
        $services = $container->findTaggedServiceIds('innmind.homeostasis.regulator');
51 10
        $map = [];
52
53 10
        foreach ($services as $id => $tags) {
54 10
            foreach ($tags as $tag => $attributes) {
55 10
                if (!isset($attributes['alias'])) {
56 2
                    throw new MissingAliasAttribute($id);
57
                }
58
59 8
                $map[$attributes['alias']] = $id;
60
            }
61
        }
62
63 8
        return $map;
64
    }
65
66 6
    private function inject(
67
        Definition $definition,
68
        string $next
69
    ) {
70 6
        $class = $definition->getClass();
71 6
        $refl = new \ReflectionMethod($class, '__construct');
72
73 6
        foreach ($refl->getParameters() as $parameter) {
74 6
            if ((string) $parameter->getType() !== Regulator::class) {
75 2
                continue;
76
            }
77
78 6
            $definition->replaceArgument(
79 6
                $parameter->getPosition(),
80 6
                new Reference($next)
81
            );
82
83 6
            return;
84
        }
85
86 2
        throw new MissingRegulatorArgument($class);
87
    }
88
}
89