RabbitMqManagerExtension::load()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.0119

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 30
cts 32
cp 0.9375
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 7
nop 2
crap 7.0119
1
<?php
2
3
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
10
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
13
class RabbitMqManagerExtension extends Extension implements PrependExtensionInterface
14
{
15
    /**
16
     * @inheritdoc
17
     */
18 4
    public function load(array $configs, ContainerBuilder $container)
19
    {
20 4
        $configuration = new Configuration();
21 4
        $config = $this->processConfiguration($configuration, $configs);
22
23 4
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
24 4
        $loader->load('services.yml');
25
26
        $configuration = [
27 4
            'path' => $config['path'],
28 4
            'consumers' => [],
29 4
            'rpc_servers' => [],
30 4
        ];
31
32 4
        $rabbitMqBundleConfiguration = $container->getParameter('mos_old_sound_rabbit_mq.config');
33
34 4
        foreach (['consumers', 'rpc_servers'] as $type) {
35 4
            if (isset($rabbitMqBundleConfiguration[$type])) {
36 3
                foreach ($rabbitMqBundleConfiguration[$type] as $name => $consumer) {
37 3
                    $consumerConfig = isset($config[$type]['individual'][$name]) ?
38 3
                        $config[$type]['individual'][$name] :
39 3
                        $config[$type]['general'];
40
41 3
                    $command = $config['commands'][$type];
42
43 3
                    if ('consumers' === $type && 'cli-consumer' === $consumerConfig['processor']) {
44
                        $command = $config['commands']['cli_consumer_invoker'];
45
                    }
46
47 3
                    $configuration[$type][$name] = $this->generateConfiguration(
48 3
                        $container,
49 3
                        $name,
50 3
                        $command,
51 3
                        $consumerConfig,
52
                        $consumer
53 3
                    );
54 2
                }
55 2
            }
56 3
        }
57
58 3
        $container->setParameter('mos_rabbitmq_cli_consumer.config', $configuration);
59 3
        $container->setParameter('mos_rabbitmq_cli_consumer.path', $configuration['path']);
60 3
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 2
    public function prepend(ContainerBuilder $container)
66
    {
67 2
        foreach ($container->getExtensions() as $name => $extension) {
68
            switch ($name) {
69 2
                case 'old_sound_rabbit_mq':
70
                    // take over this bundle's configuration
71 1
                    $extensionConfig = $container->getExtensionConfig($name);
72
73 1
                    $container->setParameter('mos_old_sound_rabbit_mq.config', $extensionConfig[0]);
74 1
                    break;
75
            }
76 2
        }
77 2
    }
78
79
    /**
80
     * @param ContainerInterface $container
81
     * @param string             $name
82
     * @param string             $command
83
     * @param array              $config
84
     * @param array              $consumer
85
     *
86
     * @return array
87
     */
88 3
    private function generateConfiguration(ContainerInterface $container, $name, $command, array $config, array $consumer)
89
    {
90 3
        $connection = $this->getConnection($container, $consumer);
91
92
        return [
93 2
            'name' => $name,
94 2
            'processor' => $config['processor'],
95 2
            'messages' => $config['messages'],
96 2
            'callback' => $consumer['callback'],
97
            'connection' => [
98 2
                'host' => $connection['host'],
99 2
                'port' => $connection['port'],
100 2
                'user' => $connection['user'],
101 2
                'password' => $connection['password'],
102 2
                'vhost' => $connection['vhost'],
103 2
            ],
104
            'supervisor' => [
105 2
                'count' => $config['worker']['count'],
106 2
                'startsecs' => $config['worker']['startsecs'],
107 2
                'autorestart' => $config['worker']['autorestart'],
108 2
                'stopsignal' => $config['worker']['stopsignal'],
109 2
                'stopwaitsecs' => $config['worker']['stopwaitsecs'],
110 2
            ],
111
            'command' => [ // todo make this configurable at some point. as for now it's not important...
112 2
                'console' => '%kernel.root_dir%/../bin/console',
113 2
                'command' => isset($config['command']['command']) ? $config['command']['command'] : $command,
114
                'arguments' => [
115 2
                    '--env=%kernel.environment%',
116 2
                    '--app=%kernel.name%',
117 2
                ],
118 2
            ],
119
            'worker' => [
120 2
                'compression' => $config['compression'],
121
                'prefetch' => [
122 2
                    'count' => isset($consumer['qos_options']['prefetch_count']) ? $consumer['qos_options']['prefetch_count'] : 0,
123 2
                    'global' => isset($consumer['qos_options']['global']) ? $consumer['qos_options']['global'] : false,
124 2
                ],
125
                'exchange' => [
126 2
                    'name' => $consumer['exchange_options']['name'],
127 2
                    'type' => $consumer['exchange_options']['type'],
128 2
                    'autodelete' => isset($consumer['exchange_options']['auto_delete']) ? $consumer['exchange_options']['auto_delete'] : false,
129 2
                    'durable' => isset($consumer['exchange_options']['durable']) ? $consumer['exchange_options']['durable'] : true,
130 2
                ],
131
                'queue' => [
132 2
                    'name' => $consumer['queue_options']['name'],
133 2
                    'routing' => isset($consumer['queue_options']['routing_keys']) ? $consumer['queue_options']['routing_keys'] : null,
134 2
                ],
135 2
            ],
136 2
        ];
137
    }
138
139
    /**
140
     * @param ContainerInterface $container
141
     * @param array              $consumer
142
     *
143
     * @return array
144
     *
145
     * @throws InvalidConfigurationException
146
     */
147 3
    private function getConnection(ContainerInterface $container, array $consumer)
148
    {
149 3
        $connections = $container->getParameter('mos_old_sound_rabbit_mq.config')['connections'];
150 3
        $name = isset($consumer['connection']) ? $consumer['connection'] : 'default';
151
152 3
        if (isset($connections[$name])) {
153 2
            return $connections[$name];
154
        }
155
156 1
        throw new InvalidConfigurationException(sprintf(
157 1
            'Connection "%s" does not exist. Available connections: %s',
158 1
            $name,
159 1
            implode(', ', array_keys($connections))
160 1
        ));
161
    }
162
}
163