Completed
Push — master ( 879c21...1c67ad )
by Tomasz
03:09
created

GendoriaCommandQueueExtension::setupManagers()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 2
crap 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 15
    public function getAlias()
27
    {
28 15
        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 15
    public function load(array $configs, ContainerBuilder $container)
39
    {
40 15
        $configuration = new Configuration($this->getAlias());
41 15
        $config = $this->processConfiguration($configuration, $configs);
42
43 14
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
44 14
        $loader->load('services.yml');
45
46 14
        $container->setParameter('gendoria_command_queue.pools', $config['pools']);
47 14
        if (!$config['enable']) {
48 1
            $managerDefinition = $container->getDefinition('gendoria_command_queue.manager');
49 1
            $managerDefinition->setClass(NullQueueManager::class);
50 1
        }
51 14
        $this->setupManagers($config, $container);
52 13
    }
53
    
54 14
    private function setupManagers($config, ContainerBuilder $container)
55
    {
56
        //Create simple managers for each pool and tag multiple manager to receive all pools
57 14
        $managerDefinition = $container->getDefinition('gendoria_command_queue.manager');
58 14
        $poolNames = array_keys($config['pools']);
59 14
        foreach ($config['pools'] as $poolName => $poolConfig) {
60 13
            $definition = new Definition(SimpleQueueManager::class);
61 13
            $definition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName));
62 13
            $container->setDefinition('gendoria_command_queue.manager.'.$poolName, $definition);
63 13
            $managerDefinition->addTag('gendoria_command_queue.send_manager', array('pool' => $poolName, 'default' => $poolName == 'default'));
64 14
        }
65
        //Inject command routing to default manager
66 14
        foreach ($config['routes'] as $commandExpression => $poolName) {
67 1
            if (!in_array($poolName, $poolNames)) {
68 1
                throw new InvalidArgumentException(sprintf("Pool \"%s\" required in command routing is not present.", $poolName));
69
            }
70 1
            $managerDefinition->addMethodCall('addCommandRoute', array($commandExpression, $poolName));
71 13
        }
72 13
    }
73
}
74