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

CommandHandlerPass::readAndForgetParameter()   A

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\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.middleware.command_handler', $builders->defaultBus()->commandHandlerMiddlewareId());
39 72
40 72
        // Wire debug command
41
        if ($container->hasDefinition('tactician.command.debug')) {
42
            $container->getDefinition('tactician.command.debug')->addArgument(array_keys($handlers));
43
        }
44 72
    }
45 72
46 72
    private function readAndForgetParameter(ContainerBuilder $container, $parameter)
47 72
    {
48
        $value = $container->getParameter($parameter);
49
        $container->getParameterBag()->remove($parameter);
50 72
51 60
        return $value;
52
    }
53
}
54