Completed
Pull Request — master (#67)
by Ross
02:01
created

TagBasedMapping   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 10 3
A mapServiceByTag() 0 16 4
isSupported() 0 1 ?
findCommandsForService() 0 1 ?
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 public function isSupported(Definition $definition, array $tagAttributes): bool;
46
47
    abstract public function findCommandsForService(Definition $definition, array $tagAttributes): array;
48
}
49