Passed
Push — master ( 1afe5c...e9653b )
by Matthew
11:56 queued 05:32
created

RabbitMQCompilerPass   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 79.45%

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 101
ccs 58
cts 73
cp 0.7945
rs 10
c 0
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setRabbitMqOptionsPt1() 0 21 5
A process() 0 20 2
A setRabbitMqOptionsPt2() 0 25 5
A setupRabbitMQOptions() 0 18 6
1
<?php
2
3
namespace Dtc\QueueBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class RabbitMQCompilerPass implements CompilerPassInterface
11
{
12 1
    public function process(ContainerBuilder $container)
13
    {
14 1
        if ($container->hasParameter('dtc_queue.rabbit_mq')) {
15 1
            $class = 'PhpAmqpLib\\Connection\\AMQPStreamConnection';
16 1
            $rabbitMqConfig = $container->getParameter('dtc_queue.rabbit_mq');
17
            $arguments = [
18 1
                $rabbitMqConfig['host'],
19 1
                $rabbitMqConfig['port'],
20 1
                $rabbitMqConfig['user'],
21 1
                $rabbitMqConfig['password'],
22 1
                $rabbitMqConfig['vhost'],
23 1
            ];
24
25 1
            $this->setupRabbitMQOptions($container, $arguments, $class);
26 1
            $definition = new Definition($class, $arguments);
27 1
            $container->setDefinition('dtc_queue.rabbit_mq', $definition);
28 1
            $definition = $container->getDefinition('dtc_queue.manager.job.rabbit_mq');
29 1
            $definition->addMethodCall('setAMQPConnection', [new Reference('dtc_queue.rabbit_mq')]);
30 1
            $definition->addMethodCall('setQueueArgs', array_values($rabbitMqConfig['queue_args']));
31 1
            $definition->addMethodCall('setExchangeArgs', array_values($rabbitMqConfig['exchange_args']));
32 1
        }
33 1
    }
34
35
    /**
36
     * @param ContainerBuilder $container
37
     * @param array            $arguments
38
     * @param string           $class
39
     */
40 1
    protected function setupRabbitMQOptions(ContainerBuilder $container, array &$arguments, &$class)
41
    {
42 1
        $rabbitMqConfig = $container->getParameter('dtc_queue.rabbit_mq');
43 1
        if (isset($rabbitMqConfig['ssl']) && $rabbitMqConfig['ssl']) {
44 1
            $class = 'PhpAmqpLib\\Connection\\AMQPSSLConnection';
45 1
            if (!empty($rabbitMqConfig['ssl_options'])) {
46 1
                $arguments[] = $rabbitMqConfig['ssl_options'];
47 1
            } else {
48 1
                $arguments[] = [];
49
            }
50 1
            if (!empty($rabbitMqConfig['options'])) {
51 1
                $arguments[] = $rabbitMqConfig['options'];
52 1
            }
53 1
        } else {
54 1
            if (!empty($rabbitMqConfig['options'])) {
55 1
                $options = $rabbitMqConfig['options'];
56 1
                $this->setRabbitMqOptionsPt1($arguments, $options);
57 1
                $this->setRabbitMqOptionsPt2($arguments, $options);
58 1
            }
59
        }
60 1
    }
61
62 1
    protected function setRabbitMqOptionsPt1(array &$arguments, array $options)
63
    {
64 1
        if (isset($options['insist'])) {
65 1
            $arguments[] = $options['insist'];
66 1
        } else {
67
            $arguments[] = false;
68
        }
69 1
        if (isset($options['login_method'])) {
70
            $arguments[] = $options['login_method'];
71
        } else {
72 1
            $arguments[] = 'AMQPLAIN';
73
        }
74 1
        if (isset($options['login_response'])) {
75
            $arguments[] = $options['login_response'];
76
        } else {
77 1
            $arguments[] = null;
78
        }
79 1
        if (isset($options['locale'])) {
80
            $arguments[] = $options['locale'];
81
        } else {
82 1
            $arguments[] = 'en_US';
83
        }
84 1
    }
85
86 1
    protected function setRabbitMqOptionsPt2(array &$arguments, array $options)
87
    {
88 1
        if (isset($options['connection_timeout'])) {
89
            $arguments[] = $options['connection_timeout'];
90
        } else {
91 1
            $arguments[] = 3.0;
92
        }
93 1
        if (isset($options['read_write_timeout'])) {
94
            $arguments[] = $options['read_write_timeout'];
95
        } else {
96 1
            $arguments[] = 3.0;
97
        }
98
99
        // Context is impossible to set through config.yml
100 1
        $arguments[] = null;
101
102 1
        if (isset($options['keepalive'])) {
103
            $arguments[] = $options['keepalive'];
104
        } else {
105 1
            $arguments[] = false;
106
        }
107 1
        if (isset($options['heartbeat'])) {
108
            $arguments[] = $options['heartbeat'];
109
        } else {
110 1
            $arguments[] = 0;
111
        }
112 1
    }
113
}
114