Completed
Pull Request — master (#20)
by
unknown
02:20
created

CommandHandlerPass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 12
Bugs 0 Features 2
Metric Value
wmc 11
c 12
b 0
f 2
lcom 1
cbo 3
dl 0
loc 109
ccs 55
cts 55
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 43 6
A getDefaultBusId() 0 6 1
A abortIfInvalidBusId() 0 8 2
A buildLocatorDefinition() 0 10 1
A buildCommandHandlerDefinition() 0 13 1
1
<?php
2
namespace League\Tactician\Bundle\DependencyInjection\Compiler;
3
4
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Definition;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
/**
10
 * This compiler pass maps Handler DI tags to specific commands
11
 */
12
class CommandHandlerPass implements CompilerPassInterface
13
{
14
    /**
15
     * You can modify the container here before it is dumped to PHP code.
16
     *
17
     * @param ContainerBuilder $container
18
     *
19
     * @throws \Exception
20
     * @api
21
     */
22 15
    public function process(ContainerBuilder $container)
23
    {
24 15
        $defaultBusId = $this->getDefaultBusId($container);
25 15
        $busIdToHandlerMapping = [];
26
27 15
        foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) {
28
29 15
            foreach ($tags as $attributes) {
30 15
                if (!isset($attributes['command'])) {
31 3
                    throw new \Exception('The tactician.handler tag must always have a command attribute');
32
                }
33
34 12
                $busId = array_key_exists('bus', $attributes) ? $attributes['bus'] : $defaultBusId;
35
36 12
                $this->abortIfInvalidBusId($busId, $container);
37
38 9
                $busIdToHandlerMapping[$busId][$attributes['command']] = $id;
39 9
            }
40 9
        }
41
42 9
        foreach ($busIdToHandlerMapping as $busId => $handlerMapping) {
43 9
            $locatorServiceId = 'tactician.commandbus.'.$busId.'.handler.locator';
44 9
            $container->setDefinition(
45 9
                $locatorServiceId,
46 9
                $this->buildLocatorDefinition($handlerMapping)
47 9
            );
48
49 9
            $container->setDefinition(
50 9
                'tactician.commandbus.'.$busId.'.middleware.command_handler',
51 9
                $this->buildCommandHandlerDefinition($locatorServiceId, $container)
52 9
            );
53 9
        }
54
55 9
        $container->setAlias(
56 9
            'tactician.handler.locator.symfony',
57 9
            'tactician.commandbus.'.$defaultBusId.'.handler.locator'
58 9
        );
59
60 9
        $container->setAlias(
61 9
            'tactician.middleware.command_handler',
62 9
            'tactician.commandbus.'.$defaultBusId.'.middleware.command_handler'
63 9
        );
64 9
    }
65
66 15
    protected function getDefaultBusId(ContainerBuilder $container)
67
    {
68 15
        $config = $container->getExtensionConfig('tactician');
69
70 15
        return $config['default_bus'];
71
    }
72
73
    /**
74
     * @param string $id
75
     * @param ContainerBuilder $container 
76
     * @throws Exception
77
     */
78 12
    protected function abortIfInvalidBusId($id, ContainerBuilder $container)
79
    {
80 12
        $config = $container->getExtensionConfig('tactician');
81
82 12
        if (!array_key_exists($id, $config['commandbus'])) {
83 3
            throw new \Exception('Invalid bus id "'.$id.'". Valid buses are: '.implode(', ', array_keys($config['commandbus'])));
84
        }
85 9
    }
86
87
    /**
88
     * @param array $handlerMapping 
89
     * @return Definition
90
     */
91 9
    protected function buildLocatorDefinition(array $handlerMapping)
92
    {
93 9
        return new Definition(
94 9
            'League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator',
95
            [
96 9
                new Reference('service_container'),
97 9
                $handlerMapping,
98
            ]
99 9
        );
100
    }
101
102
    /**
103
     * @param string $locatorServiceId 
104
     * @return Definition
105
     */
106 9
    protected function buildCommandHandlerDefinition($locatorServiceId, ContainerBuilder $container)
107
    {
108 9
        $config = $container->getExtensionConfig('tactician');
109
110 9
        return new Definition(
111 9
            'League\Tactician\Handler\CommandHandlerMiddleware',
112
            [
113 9
                new Reference('tactician.handler.command_name_extractor.class_name'),
114 9
                new Reference($locatorServiceId),
115 9
                new Reference($config['method_inflector'])
116 9
            ]
117 9
        );
118
    }
119
120
}
121