Completed
Push — master ( b81170...df9d7f )
by Danny
06:08
created

RabbitMqManagerExtension   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.62%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 7
dl 0
loc 160
ccs 88
cts 94
cp 0.9362
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C load() 0 53 9
A prepend() 0 13 3
C generateConfiguration() 0 50 7
A getConnection() 0 15 3
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 3
    public function load(array $configs, ContainerBuilder $container)
19
    {
20 3
        $configuration = new Configuration();
21 3
        $config = $this->processConfiguration($configuration, $configs);
22
23 3
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
24 3
        $loader->load('services.yml');
25
26
        // check that commands do not contain sprintf specifiers that were required by older versions
27 3
        foreach ($config['commands'] as $command) {
28 3
            if (false !== strpos($command, '%')) {
29
                throw new InvalidConfigurationException(sprintf(
30
                    'Invalid configuration for path "%s": %s',
31
                    'rabbit_mq_supervisor.commands',
32
                    'command is no longer allowed to contain sprintf specifiers (e.g. "%1$d")'
33
                ));
34
            }
35 3
        }
36
37
        $configuration = [
38 3
            'path' => $config['path'],
39 3
            'consumers' => [],
40 3
            'rpc_servers' => [],
41 3
        ];
42
43 3
        $rabbitMqBundleConfiguration = $container->getParameter('mos_old_sound_rabbit_mq.config');
44
45 3
        foreach (['consumers', 'rpc_servers'] as $type) {
46 3
            if (isset($rabbitMqBundleConfiguration[$type])) {
47 2
                foreach ($rabbitMqBundleConfiguration[$type] as $name => $consumer) {
48 2
                    $consumerConfig = isset($config[$type]['individual'][$name]) ?
49 2
                        $config[$type]['individual'][$name] :
50 2
                        $config[$type]['general'];
51
52 2
                    $command = $config['commands'][$type];
53
54 2
                    if ('consumers' === $type && 'cli-consumer' === $consumerConfig['processor']) {
55
                        $command = $config['commands']['cli_consumer_invoker'];
56
                    }
57
58 2
                    $configuration[$type][$name] = $this->generateConfiguration(
59 2
                        $container,
60 2
                        $name,
61 2
                        $command,
62 2
                        $consumerConfig,
63
                        $consumer
64 2
                    );
65 1
                }
66 1
            }
67 2
        }
68
69 2
        $container->setParameter('mos_rabbitmq_cli_consumer.config', $configuration);
70 2
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 2
    public function prepend(ContainerBuilder $container)
76
    {
77 2
        foreach ($container->getExtensions() as $name => $extension) {
78
            switch ($name) {
79 2
                case 'old_sound_rabbit_mq':
80
                    // take over this bundle's configuration
81 1
                    $extensionConfig = $container->getExtensionConfig($name);
82
83 1
                    $container->setParameter('mos_old_sound_rabbit_mq.config', $extensionConfig[0]);
84 1
                    break;
85
            }
86 2
        }
87 2
    }
88
89
    /**
90
     * @param ContainerInterface $container
91
     * @param string             $name
92
     * @param string             $command
93
     * @param array              $config
94
     * @param array              $consumer
95
     *
96
     * @return array
97
     */
98 2
    private function generateConfiguration(ContainerInterface $container, $name, $command, array $config, array $consumer)
99
    {
100 2
        $connection = $this->getConnection($container, $consumer);
101
102
        return [
103 1
            'name' => $name,
104 1
            'processor' => $config['processor'],
105 1
            'messages' => $config['messages'],
106 1
            'callback' => $consumer['callback'],
107
            'connection' => [
108 1
                'host' => $connection['host'],
109 1
                'port' => $connection['port'],
110 1
                'user' => $connection['user'],
111 1
                'password' => $connection['password'],
112 1
                'vhost' => $connection['vhost'],
113 1
            ],
114
            'supervisor' => [
115 1
                'count' => $config['worker']['count'],
116 1
                'startsecs' => $config['worker']['startsecs'],
117 1
                'autorestart' => $config['worker']['autorestart'],
118 1
                'stopsignal' => $config['worker']['stopsignal'],
119 1
                'stopwaitsecs' => $config['worker']['stopwaitsecs'],
120 1
            ],
121
            'command' => [ // todo make this configurable at some point. as for now it's not important...
122 1
                'console' => '%kernel.root_dir%/../bin/console',
123 1
                'command' => isset($config['command']['command']) ? $config['command']['command'] : $command,
124
                'arguments' => [
125 1
                    '--env=%kernel.environment%',
126 1
                    '--app=%kernel.name%',
127 1
                ],
128 1
            ],
129
            'worker' => [
130 1
                'compression' => $config['compression'],
131
                'prefetch' => [
132 1
                    'count' => isset($consumer['qos_options']['prefetch_count']) ? $consumer['qos_options']['prefetch_count'] : 0,
133 1
                    'global' => isset($consumer['qos_options']['global']) ? $consumer['qos_options']['global'] : false,
134 1
                ],
135
                'exchange' => [
136 1
                    'name' => $consumer['exchange_options']['name'],
137 1
                    'type' => $consumer['exchange_options']['type'],
138 1
                    'autodelete' => isset($consumer['exchange_options']['auto_delete']) ? $consumer['exchange_options']['auto_delete'] : false,
139 1
                    'durable' => isset($consumer['exchange_options']['durable']) ? $consumer['exchange_options']['durable'] : true,
140 1
                ],
141
                'queue' => [
142 1
                    'name' => $consumer['queue_options']['name'],
143 1
                    'routing' => isset($consumer['queue_options']['routing_keys']) ? $consumer['queue_options']['routing_keys'] : null,
144 1
                ],
145 1
            ],
146 1
        ];
147
    }
148
149
    /**
150
     * @param ContainerInterface $container
151
     * @param array              $consumer
152
     *
153
     * @return array
154
     *
155
     * @throws InvalidConfigurationException
156
     */
157 2
    private function getConnection(ContainerInterface $container, array $consumer)
158
    {
159 2
        $connections = $container->getParameter('mos_old_sound_rabbit_mq.config')['connections'];
160 2
        $name = isset($consumer['connection']) ? $consumer['connection'] : 'default';
161
162 2
        if (isset($connections[$name])) {
163 1
            return $connections[$name];
164
        }
165
166 1
        throw new InvalidConfigurationException(sprintf(
167 1
            'Connection "%s" does not exist. Available connections: %s',
168 1
            $name,
169 1
            implode(', ', array_keys($connections))
170 1
        ));
171
    }
172
}
173