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

CommandHandlerPass   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
C process() 0 46 7
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
        $defaultMapping = [];
0 ignored issues
show
Unused Code introduced by
$defaultMapping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25 15
        $defaultBusId = $this->getDefaultBusId($container);
26 15
        $busIdToHandlerMapping = [];
27
28 15
        foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) {
29
30 15
            foreach ($tags as $attributes) {
31 15
                if (!isset($attributes['command'])) {
32 3
                    throw new \Exception('The tactician.handler tag must always have a command attribute');
33
                }
34
35 12
                if (array_key_exists('bus', $attributes)) {
36 9
                    $this->abortIfInvalidBusId($attributes['bus'], $container);
37 6
                }
38
39 9
                $busId = array_key_exists('bus', $attributes) ? $attributes['bus'] : $defaultBusId;
40
41 9
                $busIdToHandlerMapping[$busId][$attributes['command']] = $id;
42 9
            }
43 9
        }
44
45 9
        foreach ($busIdToHandlerMapping as $busId => $handlerMapping) {
46 9
            $locatorServiceId = 'tactician.commandbus.'.$busId.'.handler.locator';
47 9
            $container->setDefinition(
48 9
                $locatorServiceId,
49 9
                $this->buildLocatorDefinition($handlerMapping)
50 9
            );
51
52 9
            $container->setDefinition(
53 9
                'tactician.commandbus.'.$busId.'.middleware.command_handler',
54 9
                $this->buildCommandHandlerDefinition($locatorServiceId, $container)
55 9
            );
56 9
        }
57
58 9
        $container->setAlias(
59 9
            'tactician.handler.locator.symfony',
60 9
            'tactician.commandbus.'.$defaultBusId.'.handler.locator'
61 9
        );
62
63 9
        $container->setAlias(
64 9
            'tactician.middleware.command_handler',
65 9
            'tactician.commandbus.'.$defaultBusId.'.middleware.command_handler'
66 9
        );
67 9
    }
68
69 15
    protected function getDefaultBusId(ContainerBuilder $container)
70
    {
71 15
        $config = $container->getExtensionConfig('tactician');
72
73 15
        return $config['default_bus'];
74
    }
75
76
    /**
77
     * @param string $id
78
     * @param ContainerBuilder $container 
79
     * @throws Exception
80
     */
81 9
    protected function abortIfInvalidBusId($id, ContainerBuilder $container)
82
    {
83 9
        $config = $container->getExtensionConfig('tactician');
84
85 9
        if (!array_key_exists($id, $config['commandbus'])) {
86 3
            throw new \Exception('Invalid bus id "'.$id.'". Valid buses are: '.implode(', ', array_keys($config['commandbus'])));
87
        }
88 6
    }
89
90
    /**
91
     * @param array $handlerMapping 
92
     * @return Definition
93
     */
94 9
    protected function buildLocatorDefinition(array $handlerMapping)
95
    {
96 9
        return new Definition(
97 9
            'League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator',
98
            [
99 9
                new Reference('service_container'),
100 9
                $handlerMapping,
101
            ]
102 9
        );
103
    }
104
105
    /**
106
     * @param string $locatorServiceId 
107
     * @return Definition
108
     */
109 9
    protected function buildCommandHandlerDefinition($locatorServiceId, ContainerBuilder $container)
110
    {
111 9
        $config = $container->getExtensionConfig('tactician');
112
113 9
        return new Definition(
114 9
            'League\Tactician\Handler\CommandHandlerMiddleware',
115
            [
116 9
                new Reference('tactician.handler.command_name_extractor.class_name'),
117 9
                new Reference($locatorServiceId),
118 9
                new Reference($config['method_inflector'])
119 9
            ]
120 9
        );
121
    }
122
123
}
124