Completed
Pull Request — master (#82)
by Timothée
04:41
created

CommandHandlerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 48
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B process() 0 27 3
A readAndForgetParameter() 0 7 1
1
<?php
2
3
namespace League\Tactician\Bundle\DependencyInjection\Compiler;
4
5
use League\Tactician\Bundle\DependencyInjection\Compiler\BusBuilder\BusBuildersFromConfig;
6
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\HandlerMapping;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
/**
11
 * This compiler pass maps Handler DI tags to specific commands.
12
 */
13
class CommandHandlerPass implements CompilerPassInterface
14
{
15
    /**
16
     * @var HandlerMapping
17
     */
18
    private $handlerMapping;
19
20 78
    public function __construct(HandlerMapping $mappingStrategy)
21
    {
22 78
        $this->handlerMapping = $mappingStrategy;
23 78
    }
24
25 75
    public function process(ContainerBuilder $container)
26
    {
27 75
        $builders = BusBuildersFromConfig::convert(
28 75
            $this->readAndForgetParameter($container, 'tactician.merged_config')
29
        );
30
31 75
        $routing = $this->handlerMapping->build($container, $builders->createBlankRouting());
32
33 72
        $mappings = [];
34
35
        // Register the completed builders in our container
36 72
        foreach ($builders as $builder) {
37 72
            $commandToServiceMapping = $routing->commandToServiceMapping($builder->id());
38 72
            $mappings[$builder->id()] = $commandToServiceMapping;
39 72
            $builder->registerInContainer($container, $commandToServiceMapping);
40
        }
41
42
        // Setup default aliases
43 72
        $container->setAlias('tactician.commandbus', $builders->defaultBus()->serviceId());
44 72
        $container->setAlias('tactician.handler.locator.symfony', $builders->defaultBus()->locatorServiceId());
45 72
        $container->setAlias('tactician.middleware.command_handler', $builders->defaultBus()->commandHandlerMiddlewareId());
46
47
        // Wire debug command
48 72
        if ($container->hasDefinition('tactician.command.debug')) {
49 60
            $container->getDefinition('tactician.command.debug')->addArgument($mappings);
50
        }
51 72
    }
52
53 75
    private function readAndForgetParameter(ContainerBuilder $container, $parameter)
54
    {
55 75
        $value = $container->getParameter($parameter);
56 75
        $container->getParameterBag()->remove($parameter);
57
58 75
        return $value;
59
    }
60
}
61