Completed
Pull Request — master (#67)
by Ross
05:39
created

TagBasedMapping::mapServiceByTag()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 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
    public function build(ContainerBuilder $container, Routing $routing): Routing
12
    {
13
        foreach ($container->findTaggedServiceIds('tactician.handler') as $serviceId => $tags) {
14
            foreach ($tags as $attributes) {
15
                $this->mapServiceByTag($container, $routing, $serviceId, $attributes);
16
            }
17
        }
18
19
        return $routing;
20
    }
21
22
    /**
23
     * @param ContainerBuilder $container
24
     * @param Routing $routing
25
     * @param $serviceId
26
     * @param $attributes
27
     */
28
    private function mapServiceByTag(ContainerBuilder $container, Routing $routing, $serviceId, $attributes)
29
    {
30
        $definition = $container->getDefinition($serviceId);
31
32
        if (!$this->isSupported($definition, $attributes)) {
33
            return;
34
        }
35
36
        foreach ($this->findCommandsForService($definition, $attributes) as $commandClassName) {
37
            if (isset($attributes['bus'])) {
38
                $routing->routeToBus($attributes['bus'], $commandClassName, $serviceId);
39
            } else {
40
                $routing->routeToAllBuses($commandClassName, $serviceId);
41
            }
42
        }
43
    }
44
45
    abstract function isSupported(Definition $definition, array $tagAttributes): bool;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46
47
    abstract function findCommandsForService(Definition $definition, array $tagAttributes): array;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
}