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

CommandHandlerPass::getDefaultBusId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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
                if (array_key_exists('bus', $attributes)) {
35 9
                    $this->abortIfInvalidBusId($attributes['bus'], $container);
36 6
                }
37
38 9
                $busId = array_key_exists('bus', $attributes) ? $attributes['bus'] : $defaultBusId;
39
40 9
                $busIdToHandlerMapping[$busId][$attributes['command']] = $id;
41 9
            }
42 9
        }
43
44 9
        foreach ($busIdToHandlerMapping as $busId => $handlerMapping) {
45 9
            $locatorServiceId = 'tactician.commandbus.'.$busId.'.handler.locator';
46 9
            $container->setDefinition(
47 9
                $locatorServiceId,
48 9
                $this->buildLocatorDefinition($handlerMapping)
49 9
            );
50
51 9
            $container->setDefinition(
52 9
                'tactician.commandbus.'.$busId.'.middleware.command_handler',
53 9
                $this->buildCommandHandlerDefinition($locatorServiceId, $container)
54 9
            );
55 9
        }
56
57 9
        $container->setAlias(
58 9
            'tactician.handler.locator.symfony',
59 9
            'tactician.commandbus.'.$defaultBusId.'.handler.locator'
60 9
        );
61
62 9
        $container->setAlias(
63 9
            'tactician.middleware.command_handler',
64 9
            'tactician.commandbus.'.$defaultBusId.'.middleware.command_handler'
65 9
        );
66 9
    }
67
68 15
    protected function getDefaultBusId(ContainerBuilder $container)
69
    {
70 15
        $config = $container->getExtensionConfig('tactician');
71
72 15
        return $config['default_bus'];
73
    }
74
75
    /**
76
     * @param string $id
77
     * @param ContainerBuilder $container 
78
     * @throws Exception
79
     */
80 9
    protected function abortIfInvalidBusId($id, ContainerBuilder $container)
81
    {
82 9
        $config = $container->getExtensionConfig('tactician');
83
84 9
        if (!array_key_exists($id, $config['commandbus'])) {
85 3
            throw new \Exception('Invalid bus id "'.$id.'". Valid buses are: '.implode(', ', array_keys($config['commandbus'])));
86
        }
87 6
    }
88
89
    /**
90
     * @param array $handlerMapping 
91
     * @return Definition
92
     */
93 9
    protected function buildLocatorDefinition(array $handlerMapping)
94
    {
95 9
        return new Definition(
96 9
            'League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator',
97
            [
98 9
                new Reference('service_container'),
99 9
                $handlerMapping,
100
            ]
101 9
        );
102
    }
103
104
    /**
105
     * @param string $locatorServiceId 
106
     * @return Definition
107
     */
108 9
    protected function buildCommandHandlerDefinition($locatorServiceId, ContainerBuilder $container)
109
    {
110 9
        $config = $container->getExtensionConfig('tactician');
111
112 9
        return new Definition(
113 9
            'League\Tactician\Handler\CommandHandlerMiddleware',
114
            [
115 9
                new Reference('tactician.handler.command_name_extractor.class_name'),
116 9
                new Reference($locatorServiceId),
117 9
                new Reference($config['method_inflector'])
118 9
            ]
119 9
        );
120
    }
121
122
}
123