Completed
Pull Request — master (#14)
by Jakub
02:02
created

Req2CmdExtension::configureEventListeners()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 5
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\DependencyInjection;
5
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Extension\Extension;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
final class Req2CmdExtension extends Extension
13
{
14
    /**
15
     * {@inheritdoc}
16
     * @throws \Exception
17
     */
18
    public function load(array $configs, ContainerBuilder $container): void
19
    {
20
        $configuration = new Req2CmdConfiguration();
21
        $config = $this->processConfiguration($configuration, $configs);
22
23
        $loader = new XmlFileLoader(
24
            $container,
25
            new FileLocator(__DIR__ . '/../Resources/config')
26
        );
27
28
        $loader->load('actions.xml');
29
        $loader->load('extractors.xml');
30
        $loader->load('listeners.xml');
31
        $loader->load('param_mappers.xml');
32
        $loader->load('command_bus.xml');
33
34
        $this->configureExtractors($config, $container);
35
        $this->configureCommandBus($config, $container);
36
        $this->configureEventListeners($config, $container);
37
    }
38
39
    public function getAlias(): string
40
    {
41
        return 'req2cmd';
42
    }
43
44
    private function configureExtractors(array $config, ContainerBuilder $container): void
45
    {
46
        $extractorId = (string)$config['extractor']['service_id'];
47
        $container->setAlias('eps.req2cmd.extractor', $extractorId);
48
    }
49
50
    private function configureCommandBus(array $config, ContainerBuilder $container): void
51
    {
52
        $commandBusId = (string)$config['command_bus']['service_id'];
53
        if ($commandBusId === 'eps.req2cmd.command_bus.tactician') {
54
            $busName = (string)$config['command_bus']['name'];
55
            $tacticianServiceName = 'tactician.commandbus.' . $busName;
56
            $busDefinition = $container->findDefinition('eps.req2cmd.command_bus.tactician');
57
            $busDefinition->replaceArgument(0, new Reference($tacticianServiceName));
58
        }
59
60
        $container->setAlias('eps.req2cmd.command_bus', $commandBusId);
61
    }
62
63
    private function configureEventListeners(array $config, ContainerBuilder $container): void
64
    {
65
        $listenersMap = [
66
            'extractor' => 'eps.req2cmd.listener.extract_command'
67
        ];
68
        foreach ((array)$config['listeners'] as $listenerName => $listenerConfig) {
69
            $listenerId = $listenersMap[$listenerName];
70
            if (!$listenerConfig['enabled']) {
71
                $container->removeDefinition($listenerId);
72
                continue;
73
            }
74
            $definition = $container->findDefinition($listenerId);
75
            $serviceTags = $definition->getTags();
76
            foreach ($serviceTags as $tagName => $tags) {
77
                if ($tagName !== 'kernel.event_listener') {
78
                    continue;
79
                }
80
81
                foreach ($tags as $tagIdx => $tag) {
82
                    $serviceTags[$tagName][$tagIdx]['priority'] = $listenerConfig['priority'];
83
                }
84
            }
85
            $definition->setTags($serviceTags);
86
        }
87
    }
88
}
89