Completed
Pull Request — master (#67)
by Ross
09:48
created

TagBasedMapping::mapServiceByTag()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 4
crap 4
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 99
    public function build(ContainerBuilder $container, Routing $routing): Routing
12
    {
13 99
        foreach ($container->findTaggedServiceIds('tactician.handler') as $serviceId => $tags) {
14 63
            foreach ($tags as $attributes) {
15 63
                $this->mapServiceByTag($container, $routing, $serviceId, $attributes);
16
            }
17
        }
18
19 93
        return $routing;
20
    }
21
22
    /**
23
     * @param ContainerBuilder $container
24
     * @param Routing $routing
25
     * @param $serviceId
26
     * @param $attributes
27
     */
28 63
    private function mapServiceByTag(ContainerBuilder $container, Routing $routing, $serviceId, $attributes)
29
    {
30 63
        $definition = $container->getDefinition($serviceId);
31
32 63
        if (!$this->isSupported($definition, $attributes)) {
33 24
            return;
34
        }
35
36 57
        foreach ($this->findCommandsForService($definition, $attributes) as $commandClassName) {
37 48
            if (isset($attributes['bus'])) {
38 27
                $routing->routeToBus($attributes['bus'], $commandClassName, $serviceId);
39
            } else {
40 39
                $routing->routeToAllBuses($commandClassName, $serviceId);
41
            }
42
        }
43 48
    }
44
45
    abstract protected function isSupported(Definition $definition, array $tagAttributes): bool;
46
47
    abstract protected function findCommandsForService(Definition $definition, array $tagAttributes): array;
48
}
49