Completed
Push — master ( 7b3861...ce4770 )
by Tomasz
05:13
created

GendoriaCommandQueueExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 55
ccs 24
cts 29
cp 0.8276
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 4 1
A load() 0 15 2
A setupManagers() 0 19 4
1
<?php
2
3
namespace Gendoria\CommandQueueBundle\DependencyInjection;
4
5
use Gendoria\CommandQueue\QueueManager\NullQueueManager;
6
use Gendoria\CommandQueue\QueueManager\SimpleQueueManager;
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\Loader;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
14
/**
15
 * This is the class that loads and manages your bundle configuration.
16
 *
17
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
18
 */
19
class GendoriaCommandQueueExtension extends Extension
20
{
21
    /**
22
     * Get extension alias.
23
     *
24
     * @return string
25
     */
26 4
    public function getAlias()
27
    {
28 4
        return 'gendoria_command_queue';
29
    }
30
31
    /**
32
     * Load extension.
33
     *
34
     * @param array            $configs
35
     * @param ContainerBuilder $container
36
     * @throws InvalidArgumentException Thrown, when pool required in configuration is not present.
37
     */
38 4
    public function load(array $configs, ContainerBuilder $container)
39
    {
40 4
        $configuration = new Configuration($this->getAlias());
41 4
        $config = $this->processConfiguration($configuration, $configs);
42
43 3
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
44 3
        $loader->load('services.yml');
45
46 3
        $container->setParameter('gendoria_command_queue.pools', $config['pools']);
47 3
        if (!$config['enable']) {
48
            $managerDefinition = $container->getDefinition('gendoria_command_queue.manager');
49
            $managerDefinition->setClass(NullQueueManager::class);
50
        }
51 3
        $this->setupManagers($config, $container);
52 3
    }
53
    
54 3
    private function setupManagers($config, ContainerBuilder $container)
55
    {
56
        //Create simple managers for each pool and tag multiple manager to receive all pools
57 3
        $managerDefinition = $container->getDefinition('gendoria_command_queue.manager');
58 3
        $poolNames = array_keys($config['pools']);
59 3
        foreach ($config['pools'] as $poolName => $poolConfig) {
60 3
            $definition = new Definition(SimpleQueueManager::class);
61 3
            $definition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName));
62 3
            $container->setDefinition('gendoria_command_queue.manager.'.$poolName, $definition);
63 3
            $managerDefinition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName, 'default' => $poolName == 'default'));
64 3
        }
65
        //Inject command routing to default manager
66 3
        foreach ($config['routes'] as $commandExpression => $poolName) {
67
            if (!in_array($poolName, $poolNames)) {
68
                throw new InvalidArgumentException(sprintf("Pool \"%s\" required in command routing is not present", $poolName));
69
            }
70 1
            $managerDefinition->addMethodCall('addCommandRoute', array($commandExpression, $poolName));
71 3
        }
72 3
    }
73
}
74