Completed
Push — master ( 80fc31...ca39d5 )
by Danny
02:45
created

RabbitMqManagerExtension::getConnection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 2
crap 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 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
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 2
    public function prepend(ContainerBuilder $container)
65
    {
66 2
        foreach ($container->getExtensions() as $name => $extension) {
67
            switch ($name) {
68 2
                case 'old_sound_rabbit_mq':
69
                    // take over this bundle's configuration
70 1
                    $extensionConfig = $container->getExtensionConfig($name);
71
72 1
                    $container->setParameter('mos_old_sound_rabbit_mq.config', $extensionConfig[0]);
73 1
                    break;
74
            }
75 2
        }
76 2
    }
77
78
    /**
79
     * @param ContainerInterface $container
80
     * @param string             $name
81
     * @param string             $command
82
     * @param array              $config
83
     * @param array              $consumer
84
     *
85
     * @return array
86
     */
87 3
    private function generateConfiguration(ContainerInterface $container, $name, $command, array $config, array $consumer)
88
    {
89 3
        $connection = $this->getConnection($container, $consumer);
90
91
        return [
92 2
            'name' => $name,
93 2
            'processor' => $config['processor'],
94 2
            'messages' => $config['messages'],
95 2
            'callback' => $consumer['callback'],
96
            'connection' => [
97 2
                'host' => $connection['host'],
98 2
                'port' => $connection['port'],
99 2
                'user' => $connection['user'],
100 2
                'password' => $connection['password'],
101 2
                'vhost' => $connection['vhost'],
102 2
            ],
103
            'supervisor' => [
104 2
                'count' => $config['worker']['count'],
105 2
                'startsecs' => $config['worker']['startsecs'],
106 2
                'autorestart' => $config['worker']['autorestart'],
107 2
                'stopsignal' => $config['worker']['stopsignal'],
108 2
                'stopwaitsecs' => $config['worker']['stopwaitsecs'],
109 2
            ],
110
            'command' => [ // todo make this configurable at some point. as for now it's not important...
111 2
                'console' => '%kernel.root_dir%/../bin/console',
112 2
                'command' => isset($config['command']['command']) ? $config['command']['command'] : $command,
113
                'arguments' => [
114 2
                    '--env=%kernel.environment%',
115 2
                    '--app=%kernel.name%',
116 2
                ],
117 2
            ],
118
            'worker' => [
119 2
                'compression' => $config['compression'],
120
                'prefetch' => [
121 2
                    'count' => isset($consumer['qos_options']['prefetch_count']) ? $consumer['qos_options']['prefetch_count'] : 0,
122 2
                    'global' => isset($consumer['qos_options']['global']) ? $consumer['qos_options']['global'] : false,
123 2
                ],
124
                'exchange' => [
125 2
                    'name' => $consumer['exchange_options']['name'],
126 2
                    'type' => $consumer['exchange_options']['type'],
127 2
                    'autodelete' => isset($consumer['exchange_options']['auto_delete']) ? $consumer['exchange_options']['auto_delete'] : false,
128 2
                    'durable' => isset($consumer['exchange_options']['durable']) ? $consumer['exchange_options']['durable'] : true,
129 2
                ],
130
                'queue' => [
131 2
                    'name' => $consumer['queue_options']['name'],
132 2
                    'routing' => isset($consumer['queue_options']['routing_keys']) ? $consumer['queue_options']['routing_keys'] : null,
133 2
                ],
134 2
            ],
135 2
        ];
136
    }
137
138
    /**
139
     * @param ContainerInterface $container
140
     * @param array              $consumer
141
     *
142
     * @return array
143
     *
144
     * @throws InvalidConfigurationException
145
     */
146 3
    private function getConnection(ContainerInterface $container, array $consumer)
147
    {
148 3
        $connections = $container->getParameter('mos_old_sound_rabbit_mq.config')['connections'];
149 3
        $name = isset($consumer['connection']) ? $consumer['connection'] : 'default';
150
151 3
        if (isset($connections[$name])) {
152 2
            return $connections[$name];
153
        }
154
155 1
        throw new InvalidConfigurationException(sprintf(
156 1
            'Connection "%s" does not exist. Available connections: %s',
157 1
            $name,
158 1
            implode(', ', array_keys($connections))
159 1
        ));
160
    }
161
}
162