Completed
Push — master ( 70c29b...7d604a )
by Tomasz
06:29
created

prepend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 64
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 46
cts 46
cp 1
rs 9.3956
c 0
b 0
f 0
cc 2
eloc 43
nc 2
nop 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Gendoria\CommandQueueRabbitMqDriverBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration.
14
 *
15
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
16
 */
17
class GendoriaCommandQueueRabbitMqDriverExtension extends Extension implements PrependExtensionInterface
18
{
19
    /**
20
     * Get extension alias.
21
     *
22
     * @return string
23
     */
24 2
    public function getAlias()
25
    {
26 2
        return 'gendoria_command_queue_rabbit_mq_driver';
27
    }
28
29
    /**
30
     * Load extension.
31
     *
32
     * @param array            $configs
33
     * @param ContainerBuilder $container
34
     */
35 1
    public function load(array $configs, ContainerBuilder $container)
36
    {
37 1
        $configuration = new Configuration($this->getAlias());
38 1
        $config = $this->processConfiguration($configuration, $configs);
39
40 1
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
41 1
        $loader->load('services.yml');
42
43 1
        $this->loadDrivers($config, $container);
44 1
        $container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.send_driver');
45 1
        $container->removeDefinition('gendoria_command_queue_rabbit_mq_driver.external_data_worker');
46 1
    }
47
    
48 1
    private function loadDrivers(array $config, ContainerBuilder $container)
49
    {
50 1
        $serializer = substr($config['serializer'], 1);
51 1
        foreach ($config['drivers'] as $driverId => $driver) {
52 1
            $producerName = sprintf('old_sound_rabbit_mq.%s_producer', $driver['producer_name']);
53 1
            $delayedProducerName = sprintf('old_sound_rabbit_mq.%s_reschedule_delayed_producer', $driver['producer_name']);
54
            
55 1
            $newDriver = clone $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.send_driver');
56 1
            $newDriver->replaceArgument(0, new Reference($serializer));
57 1
            $newDriver->replaceArgument(1, new Reference($producerName));
58 1
            $container->setDefinition('gendoria_command_queue_rabbit_mq_driver.driver.'.$driverId, $newDriver);
59
60 1
            $newWorker = clone $container->getDefinition('gendoria_command_queue_rabbit_mq_driver.external_data_worker');
61 1
            $newWorker->replaceArgument(2, new Reference($serializer));
62 1
            $newWorker->replaceArgument(3, new Reference($delayedProducerName));
63 1
            $container->setDefinition('gendoria_command_queue_rabbit_mq_driver.worker.'.$driverId, $newWorker);
64 1
        }
65 1
    }
66
67
    /**
68
     * Prepend configuration to OldSoundRabbitMQBundle.
69
     *
70
     * @param ContainerBuilder $container
71
     */
72 2
    public function prepend(ContainerBuilder $container)
73
    {
74 1
        $configs = $container->getExtensionConfig($this->getAlias());
75 1
        $currentConfig = $this->processConfiguration(new Configuration($this->getAlias()), $configs);
76
77
        $rabbitMQConfig = array(
78 1
            'consumers' => array(),
79 1
            'producers' => array(),
80 1
        );
81
82 1
        foreach ($currentConfig['drivers'] as $driverId => $driver) {
83
            $exchangeOptions = array(
84 1
                'name' => $driver['exchange_name'],
85 1
                'type' => 'topic',
86 1
            );
87
88 1
            $rabbitMQConfig['producers'][$driver['producer_name']] = array(
89 1
                'connection' => $driver['rabbitmq_connection'],
90 1
                'exchange_options' => $exchangeOptions,
91
            );
92
93 1
            $rabbitMQConfig['consumers'][$driver['consumer_name']] = array(
94 1
                'connection' => $driver['rabbitmq_connection'],
95 1
                'exchange_options' => $exchangeOptions,
96
                'queue_options' => array(
97 2
                    'name' => $driver['consumer_queue_name'],
98 1
                    'durable' => true,
99 1
                    'auto_delete' => false,
100 1
                    'routing_keys' => array('*'),
101 1
                ),
102
                'qos_options' => array(
103 1
                    'prefetch_size' => 0,
104 1
                    'prefetch_count' => 1,
105 1
                    'global' => false,
106 1
                ),
107 1
                'callback' => 'gendoria_command_queue_rabbit_mq_driver.worker.'.$driverId,
108
            );
109
110 1
            $rabbitMQConfig['producers'][$driver['producer_name'].'_reschedule_delayed'] = array(
111 1
                'connection' => $driver['rabbitmq_connection'],
112 1
                'exchange_options' => array_merge($exchangeOptions, array(
113 1
                    'name' => $driver['exchange_name'].'-reschedule-delayed',
114 1
                )),
115
            );
116
117 1
            $rabbitMQConfig['consumers'][$driver['consumer_name'].'_reschedule_delayed'] = array(
118 1
                'connection' => $driver['rabbitmq_connection'],
119 1
                'exchange_options' => $rabbitMQConfig['producers'][$driver['producer_name'].'_reschedule_delayed']['exchange_options'],
120
                'queue_options' => array(
121 1
                    'name' => $driver['consumer_queue_name'].'-reschedule-delayed',
122 1
                    'durable' => true,
123 1
                    'auto_delete' => false,
124 1
                    'routing_keys' => array('*'),
125
                    'arguments' => array(
126 1
                        'x-dead-letter-exchange' => array('S', $exchangeOptions['name']),
127 1
                        'x-message-ttl' => array('I', 600000), //10 minutes
128 1
                    ),
129 1
                ),
130 1
                'callback' => 'gendoria_command_queue_rabbit_mq_driver.services_reschedule_worker',
131
            );
132 1
        }
133
134 1
        $container->prependExtensionConfig('old_sound_rabbit_mq', $rabbitMQConfig);
135 1
    }
136
}
137