CommandHandlerPass::readAndForgetParameter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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
use League\Tactician\CommandBus;
10
11
/**
12
 * This compiler pass maps Handler DI tags to specific commands.
13
 */
14
class CommandHandlerPass implements CompilerPassInterface
15
{
16
    /**
17
     * @var HandlerMapping
18
     */
19
    private $handlerMapping;
20
21 78
    public function __construct(HandlerMapping $mappingStrategy)
22
    {
23 78
        $this->handlerMapping = $mappingStrategy;
24 78
    }
25
26 75
    public function process(ContainerBuilder $container)
27
    {
28 75
        $builders = BusBuildersFromConfig::convert(
29 75
            $this->readAndForgetParameter($container, 'tactician.merged_config')
30
        );
31
32 75
        $routing = $this->handlerMapping->build($container, $builders->createBlankRouting());
33
34 72
        $mappings = [];
35
36
        // Register the completed builders in our container
37 72
        foreach ($builders as $builder) {
38 72
            $commandToServiceMapping = $routing->commandToServiceMapping($builder->id());
39 72
            $mappings[$builder->id()] = $commandToServiceMapping;
40 72
            $builder->registerInContainer($container, $commandToServiceMapping);
41
        }
42
43
        // Setup default aliases
44 72
        $container->setAlias('tactician.commandbus', $builders->defaultBus()->serviceId());
45 72
        $container->setAlias(CommandBus::class, 'tactician.commandbus');
46 72
        $container->setAlias('tactician.handler.locator.symfony', $builders->defaultBus()->locatorServiceId());
47 72
        $container->setAlias('tactician.middleware.command_handler', $builders->defaultBus()->commandHandlerMiddlewareId());
48
49
        // Wire debug command
50 72
        if ($container->hasDefinition('tactician.command.debug')) {
51 60
            $container->getDefinition('tactician.command.debug')->addArgument($mappings);
52
        }
53 72
    }
54
55 75
    private function readAndForgetParameter(ContainerBuilder $container, $parameter)
56
    {
57 75
        $value = $container->getParameter($parameter);
58 75
        $container->getParameterBag()->remove($parameter);
59
60 75
        return $value;
61
    }
62
}
63