Test Failed
Pull Request — master (#3)
by Danny
04:50
created

RabbitMqManagerExtension::load()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.7656

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
ccs 24
cts 32
cp 0.75
rs 6.7272
cc 7
eloc 27
nc 7
nop 2
crap 7.7656
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 1
            'path' => $config['path'],
28 1
            'consumers' => [],
29 1
            'rpc_servers' => [],
30 1
        ];
31
32 1
        $rabbitMqBundleConfiguration = $container->getParameter('mos_old_sound_rabbit_mq.config');
33
34 1
        foreach (['consumers', 'rpc_servers'] as $type) {
35 1
            if (isset($rabbitMqBundleConfiguration[$type])) {
36 1
                foreach ($rabbitMqBundleConfiguration[$type] as $name => $consumer) {
37 1
                    $consumerConfig = isset($config[$type]['individual'][$name]) ?
38 1
                        $config[$type]['individual'][$name] :
39 1
                        $config[$type]['general'];
40
41 1
                    $command = $config['commands'][$type];
42
43 1
                    if ('consumers' === $type && 'cli-consumer' === $consumerConfig['processor']) {
44
                        $command = $config['commands']['cli_consumer_invoker'];
45
                    }
46
47 1
                    $configuration[$type][$name] = $this->generateConfiguration(
48 1
                        $container,
49 1
                        $name,
50 1
                        $command,
51 1
                        $consumerConfig,
52
                        $consumer
53 1
                    );
54
                }
55
            }
56
        }
57
58
        $container->setParameter('mos_rabbitmq_cli_consumer.config', $configuration);
59
        $container->setParameter('mos_rabbitmq_cli_consumer.path', $configuration['path']);
60
    }
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 1
    private function generateConfiguration(ContainerInterface $container, $name, $command, array $config, array $consumer)
89
    {
90 1
        $connection = $this->getConnection($container, $consumer);
91
92
        return [
93
            'name' => $name,
94 1
            'processor' => $config['processor'],
95
            'messages' => $config['messages'],
96
            'callback' => $consumer['callback'],
97
            'connection' => [
98
                'host' => $connection['host'],
99
                'port' => $connection['port'],
100
                'user' => $connection['user'],
101
                'password' => $connection['password'],
102
                'vhost' => $connection['vhost'],
103
            ],
104
            'supervisor' => [
105
                'count' => $config['worker']['count'],
106
                'startsecs' => $config['worker']['startsecs'],
107
                'autorestart' => $config['worker']['autorestart'],
108
                'stopsignal' => $config['worker']['stopsignal'],
109
                'stopwaitsecs' => $config['worker']['stopwaitsecs'],
110
            ],
111
            'command' => [ // todo make this configurable at some point. as for now it's not important...
112
                'console' => '%kernel.root_dir%/../bin/console',
113
                'command' => isset($config['command']['command']) ? $config['command']['command'] : $command,
114
                'arguments' => [
115
                    '--env=%kernel.environment%',
116
                    '--app=%kernel.name%',
117
                ],
118
            ],
119
            'worker' => [
120
                'compression' => $config['compression'],
121
                'prefetch' => [
122
                    'count' => isset($consumer['qos_options']['prefetch_count']) ? $consumer['qos_options']['prefetch_count'] : 0,
123
                    'global' => isset($consumer['qos_options']['global']) ? $consumer['qos_options']['global'] : false,
124
                ],
125
                'exchange' => [
126
                    'name' => $consumer['exchange_options']['name'],
127
                    'type' => $consumer['exchange_options']['type'],
128
                    'autodelete' => isset($consumer['exchange_options']['auto_delete']) ? $consumer['exchange_options']['auto_delete'] : false,
129
                    'durable' => isset($consumer['exchange_options']['durable']) ? $consumer['exchange_options']['durable'] : true,
130
                ],
131
                'queue' => [
132
                    'name' => $consumer['queue_options']['name'],
133
                    'routing' => isset($consumer['queue_options']['routing_keys']) ? $consumer['queue_options']['routing_keys'] : null,
134
                ],
135
            ],
136
        ];
137
    }
138
139
    /**
140
     * @param ContainerInterface $container
141
     * @param array              $consumer
142
     *
143
     * @return array
144
     *
145
     * @throws InvalidConfigurationException
146
     */
147 1
    private function getConnection(ContainerInterface $container, array $consumer)
148
    {
149 1
        $connections = $container->getParameter('mos_old_sound_rabbit_mq.config')['connections'];
150 1
        $name = isset($consumer['connection']) ? $consumer['connection'] : 'default';
151
152 1
        if (isset($connections[$name])) {
153
            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