TagBasedMapping::build()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping;
5
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
9
abstract class TagBasedMapping implements HandlerMapping
10
{
11
    const TAG_NAME = 'tactician.handler';
12
13 132
    public function build(ContainerBuilder $container, Routing $routing): Routing
14
    {
15 132
        foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $serviceId => $tags) {
16 96
            foreach ($tags as $attributes) {
17 96
                $this->mapServiceByTag($container, $routing, $serviceId, $attributes);
18
            }
19
        }
20
21 126
        return $routing;
22
    }
23
24
    /**
25
     * @param ContainerBuilder $container
26
     * @param Routing $routing
27
     * @param $serviceId
28
     * @param $attributes
29
     */
30 96
    private function mapServiceByTag(ContainerBuilder $container, Routing $routing, $serviceId, $attributes)
31
    {
32 96
        $definition = $container->getDefinition($serviceId);
33
34 96
        if (!$this->isSupported($container, $definition, $attributes)) {
35 36
            return;
36
        }
37
38 90
        foreach ($this->findCommandsForService($container, $definition, $attributes) as $commandClassName) {
39 66
            if (isset($attributes['bus'])) {
40 27
                $routing->routeToBus($attributes['bus'], $commandClassName, $serviceId);
41
            } else {
42 51
                $routing->routeToAllBuses($commandClassName, $serviceId);
43
            }
44
        }
45 81
    }
46
47
    abstract protected function isSupported(ContainerBuilder $container, Definition $definition, array $tagAttributes): bool;
48
49
    abstract protected function findCommandsForService(ContainerBuilder $container, Definition $definition, array $tagAttributes): array;
50
}
51