Completed
Push — master ( d322ef...2009cd )
by Baptiste
03:15 queued 01:06
created

BuildCommandBusStackPass::searchServices()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 9
Ratio 52.94 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 9
loc 17
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\CommandBusBundle\DependencyInjection\Compiler;
5
6
use Innmind\CommandBusBundle\Exception\LogicException;
7
use Innmind\CommandBus\CommandBusInterface;
8
use Symfony\Component\DependencyInjection\{
9
    Compiler\CompilerPassInterface,
10
    ContainerBuilder,
11
    Definition,
12
    Reference
13
};
14
15
final class BuildCommandBusStackPass implements CompilerPassInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 6
    public function process(ContainerBuilder $container)
21
    {
22 6
        if (!$container->hasParameter('innmind_command_bus.stack')) {
23 1
            throw new LogicException;
24
        }
25
26 5
        $stack = $container->getParameter('innmind_command_bus.stack');
27 5
        $services = $this->searchServices($container);
28
29 4
        $container->setAlias(
30 4
            'innmind_command_bus',
31 4
            $services[$stack[0]]
32
        );
33
34 4
        if (count($stack) === 1) {
35 1
            return;
36
        }
37
38 3
        for ($i = 0, $count = count($stack) - 1; $i < $count; $i++) {
39 3
            $alias = $stack[$i];
40 3
            $next = $stack[$i + 1];
41
42 3
            $this->inject(
43 3
                $container->getDefinition($services[$alias]),
44 3
                $services[$next]
45
            );
46
        }
47 2
    }
48
49 5
    private function searchServices(ContainerBuilder $container): array
50
    {
51 5
        $services = $container->findTaggedServiceIds('innmind_command_bus');
52 5
        $map = [];
53
54 5 View Code Duplication
        foreach ($services as $id => $tags) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55 5
            foreach ($tags as $tag => $attributes) {
56 5
                if (!isset($attributes['alias'])) {
57 1
                    throw new LogicException;
58
                }
59
60 4
                $map[$attributes['alias']] = $id;
61
            }
62
        }
63
64 4
        return $map;
65
    }
66
67 3
    private function inject(
68
        Definition $definition,
69
        string $next
70
    ) {
71 3
        $class = $definition->getClass();
72 3
        $refl = new \ReflectionMethod($class, '__construct');
73
74 3
        foreach ($refl->getParameters() as $parameter) {
75 3
            if ((string) $parameter->getType() !== CommandBusInterface::class) {
76 1
                continue;
77
            }
78
79 3
            $definition->replaceArgument(
80 3
                $parameter->getPosition(),
81 3
                new Reference($next)
82
            );
83
84 3
            return;
85
        }
86
87 1
        throw new LogicException(sprintf(
88 1
            'Missing argument type hinted with CommandBusInterface for "%s"',
89
            $class
90
        ));
91
    }
92
}
93