Completed
Pull Request — master (#115)
by Šimon
01:50
created

CommandHandlerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 28 4
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\CommandBus;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use function array_keys;
10
11
/**
12
 * This compiler pass maps Handler DI tags to specific commands.
13
 */
14
class CommandHandlerPass implements CompilerPassInterface
15
{
16
    public const TACTICIAN_HANDLER_TAG = 'tactician.handler';
17
18
    public function process(ContainerBuilder $container) : void
19
    {
20
        $handlers = $container->findTaggedServiceIds(self::TACTICIAN_HANDLER_TAG);
21 78
        foreach ($handlers as $handler => $_) {
22
            $definition = $container->findDefinition($handler);
23 78
            $definition->setPublic(true);
24 78
        }
25
26 75
        $builders = BusBuildersFromConfig::convert(
27
            $this->readAndForgetParameter($container, 'tactician.merged_config')
28 75
        );
29 75
30
        // Register the completed builders in our container
31
        foreach ($builders as $builder) {
32 75
            $builder->registerInContainer($container);
33
        }
34 72
35
        // Setup default aliases
36
        $container->setAlias('tactician.commandbus', $builders->defaultBus()->serviceId());
37 72
        $container->setAlias(CommandBus::class, 'tactician.commandbus');
38 72
        $container->setAlias('tactician.handler.locator.symfony', $builders->defaultBus()->locatorServiceId());
39 72
        $container->setAlias('tactician.middleware.command_handler', $builders->defaultBus()->commandHandlerMiddlewareId());
40 72
41
        // Wire debug command
42
        if ($container->hasDefinition('tactician.command.debug')) {
43
            $container->getDefinition('tactician.command.debug')->addArgument(array_keys($handlers));
44 72
        }
45 72
    }
46 72
47 72
    private function readAndForgetParameter(ContainerBuilder $container, $parameter)
48
    {
49
        $value = $container->getParameter($parameter);
50 72
        $container->getParameterBag()->remove($parameter);
51 60
52
        return $value;
53 72
    }
54
}
55