Completed
Push — master ( c702e0...af42cc )
by Matthew
07:24
created

RabbitMQCompilerPass::setRabbitMqOptionsPt2()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 22
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 32
nop 2
crap 42
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.job_manager.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
                $arguments[] = $rabbitMqConfig['ssl_options'];
47
            } else {
48 1
                $arguments[] = [];
49
            }
50 1
            if (!empty($rabbitMqConfig['options'])) {
51
                $arguments[] = $rabbitMqConfig['options'];
52
            }
53 1
        } else {
54 1
            if (!empty($rabbitMqConfig['options'])) {
55
                $options = $rabbitMqConfig['options'];
56
                $this->setRabbitMqOptionsPt1($arguments, $options);
57
                $this->setRabbitMqOptionsPt2($arguments, $options);
58
            }
59
        }
60 1
    }
61
62
    protected function setRabbitMqOptionsPt1(array &$arguments, array $options)
63
    {
64
        if (isset($options['insist'])) {
65
            $arguments[] = $options['insist'];
66
        } else {
67
            $arguments[] = false;
68
        }
69
        if (isset($options['login_method'])) {
70
            $arguments[] = $options['login_method'];
71
        } else {
72
            $arguments[] = 'AMQPLAIN';
73
        }
74
        if (isset($options['login_response'])) {
75
            $arguments[] = $options['login_response'];
76
        } else {
77
            $arguments[] = null;
78
        }
79
        if (isset($options['locale'])) {
80
            $arguments[] = $options['locale'];
81
        } else {
82
            $arguments[] = 'en_US';
83
        }
84
    }
85
86
    protected function setRabbitMqOptionsPt2(array &$arguments, array $options)
87
    {
88
        if (isset($options['connection_timeout'])) {
89
            $arguments[] = $options['connection_timeout'];
90
        } else {
91
            $arguments[] = 3.0;
92
        }
93
        if (isset($options['read_write_timeout'])) {
94
            $arguments[] = $options['read_write_timeout'];
95
        } else {
96
            $arguments[] = 3.0;
97
        }
98
        if (isset($options['context'])) {
99
            $arguments[] = $options['context'];
100
        } else {
101
            $arguments[] = null;
102
        }
103
        if (isset($options['keepalive'])) {
104
            $arguments[] = $options['keepalive'];
105
        } else {
106
            $arguments[] = false;
107
        }
108
        if (isset($options['heartbeat'])) {
109
            $arguments[] = $options['heartbeat'];
110
        } else {
111
            $arguments[] = 0;
112
        }
113
    }
114
}
115