Completed
Push — master ( 22c320...7c1e88 )
by Tomasz
11:02
created

GendoriaCommandQueueExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 87
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 4 1
A load() 0 18 3
A setupManagers() 0 19 4
A prepend() 0 4 1
A disableDoctrineListener() 0 12 3
1
<?php
2
3
namespace Gendoria\CommandQueueBundle\DependencyInjection;
4
5
use Gendoria\CommandQueue\QueueManager\NullQueueManager;
6
use Gendoria\CommandQueue\QueueManager\SingleQueueManager;
7
use InvalidArgumentException;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Definition;
11
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
12
use Symfony\Component\DependencyInjection\Loader;
13
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
14
15
/**
16
 * This is the class that loads and manages your bundle configuration.
17
 *
18
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
19
 */
20
class GendoriaCommandQueueExtension extends Extension implements PrependExtensionInterface
21
{
22
    /**
23
     * Get extension alias.
24
     *
25
     * @return string
26
     */
27 9
    public function getAlias()
28
    {
29 9
        return 'gendoria_command_queue';
30
    }
31
32
    /**
33
     * Load extension.
34
     *
35
     * @param array            $configs
36
     * @param ContainerBuilder $container
37
     * @throws InvalidArgumentException Thrown, when pool required in configuration is not present.
38
     */
39 9
    public function load(array $configs, ContainerBuilder $container)
40
    {
41 9
        $configuration = new Configuration($this->getAlias());
42 9
        $config = $this->processConfiguration($configuration, $configs);
43
44 8
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
45 8
        $loader->load('services.yml');
46
47 8
        $container->setParameter('gendoria_command_queue.pools', $config['pools']);
48 8
        if (!$config['enabled']) {
49 1
            $managerDefinition = $container->getDefinition('gendoria_command_queue.manager');
50 1
            $managerDefinition->setClass(NullQueueManager::class);
51 1
        }
52 8
        $this->setupManagers($config, $container);
53 7
        if (!$config['clear_entity_managers_listener_enabled']) {
54 1
            $container->removeDefinition('gendoria_command_queue.listener.clear_entity_managers');
55 1
        }        
56 7
    }
57
    
58 8
    private function setupManagers($config, ContainerBuilder $container)
59
    {
60
        //Create simple managers for each pool and tag multiple manager to receive all pools
61 8
        $managerDefinition = $container->getDefinition('gendoria_command_queue.manager');
62 8
        $poolNames = array_keys($config['pools']);
63 8
        foreach ($config['pools'] as $poolName => $poolConfig) {
64 7
            $definition = new Definition(SingleQueueManager::class);
65 7
            $definition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName));
66 7
            $container->setDefinition('gendoria_command_queue.manager.'.$poolName, $definition);
67 7
            $managerDefinition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName, 'default' => $poolName == 'default'));
68 8
        }
69
        //Inject command routing to default manager
70 8
        foreach ($config['routes'] as $commandExpression => $poolName) {
71 2
            if (!in_array($poolName, $poolNames)) {
72 1
                throw new InvalidArgumentException(sprintf("Pool \"%s\" required in command routing is not present.", $poolName));
73
            }
74 1
            $managerDefinition->addMethodCall('addCommandRoute', array($commandExpression, $poolName));
75 7
        }
76 7
    }
77
    
78
    /**
79
     * Prepend configuration.
80
     *
81
     * @param ContainerBuilder $container
82
     */
83 2
    public function prepend(ContainerBuilder $container)
84
    {
85 2
        $this->disableDoctrineListener($container);
86 2
    }
87
    
88
    /**
89
     * Disable clear entity managers listener, if no doctrine bundle is installed.
90
     * 
91
     * @param ContainerBuilder $container
92
     * @return void
93
     */
94 2
    private function disableDoctrineListener(ContainerBuilder $container)
95
    {
96 2
        if (!$container->hasParameter('kernel.bundles')) {
97 2
            return;
98
        }
99 1
        $bundles = $container->getParameter('kernel.bundles');
100 1
        if (!isset($bundles['DoctrineBundle'])) {
101 1
            $container->prependExtensionConfig($this->getAlias(), array(
102 1
                'clear_entity_managers_listener_enabled' => false,
103 1
            ));
104 1
        }
105 1
    }   
106
}
107