TagBasedMapping   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 42
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
isSupported() 0 1 ?
findCommandsForService() 0 1 ?
A build() 0 10 3
A mapServiceByTag() 0 16 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
    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