Completed
Push — master ( 17631a...07adb9 )
by Matthew
05:11
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 28
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
    public function process(ContainerBuilder $container)
13
    {
14
        if ($container->hasParameter('dtc_queue.rabbit_mq')) {
15
            $class = 'PhpAmqpLib\\Connection\\AMQPStreamConnection';
16
            $rabbitMqConfig = $container->getParameter('dtc_queue.rabbit_mq');
17
            $arguments = [
18
                $rabbitMqConfig['host'],
19
                $rabbitMqConfig['port'],
20
                $rabbitMqConfig['user'],
21
                $rabbitMqConfig['password'],
22
                $rabbitMqConfig['vhost'],
23
            ];
24
25
            $this->setupRabbitMQOptions($container, $arguments, $class);
26
            $definition = new Definition($class, $arguments);
27
            $container->setDefinition('dtc_queue.rabbit_mq', $definition);
28
            $definition = $container->getDefinition('dtc_queue.job_manager.rabbit_mq');
29
            $definition->addMethodCall('setAMQPConnection', [new Reference('dtc_queue.rabbit_mq')]);
30
            $definition->addMethodCall('setQueueArgs', array_values($rabbitMqConfig['queue_args']));
31
            $definition->addMethodCall('setExchangeArgs', array_values($rabbitMqConfig['exchange_args']));
32
        }
33
    }
34
35
    /**
36
     * @param ContainerBuilder $container
37
     * @param array            $arguments
38
     * @param string           $class
39
     */
40
    protected function setupRabbitMQOptions(ContainerBuilder $container, array &$arguments, &$class)
41
    {
42
        if ($container->hasParameter('dtc_queue.rabbit_mq.ssl') && $container->getParameter('dtc_queue.rabbit_mq.ssl')) {
43
            $class = 'PhpAmqpLib\\Connection\\AMQPSSLConnection';
44
            if ($container->hasParameter('dtc_queue.rabbit_mq.ssl_options')) {
45
                $arguments[] = $container->getParameter('dtc_queue.rabbit_mq.ssl_options');
46
            } else {
47
                $arguments[] = [];
48
            }
49
            if ($container->hasParameter('dtc_queue.rabbit_mq.options')) {
50
                $arguments[] = $container->getParameter('dtc_queue.rabbit_mq.options');
51
            }
52
        } else {
53
            if ($container->hasParameter('dtc_queue.rabbit_mq.options')) {
54
                $options = $container->getParameter('dtc_queue.rabbit_mq.options');
55
                $this->setRabbitMqOptionsPt1($arguments, $options);
56
                $this->setRabbitMqOptionsPt2($arguments, $options);
57
            }
58
        }
59
    }
60
61
    protected function setRabbitMqOptionsPt1(array &$arguments, array $options)
62
    {
63
        if (isset($options['insist'])) {
64
            $arguments[] = $options['insist'];
65
        } else {
66
            $arguments[] = false;
67
        }
68
        if (isset($options['login_method'])) {
69
            $arguments[] = $options['login_method'];
70
        } else {
71
            $arguments[] = 'AMQPLAIN';
72
        }
73
        if (isset($options['login_response'])) {
74
            $arguments[] = $options['login_response'];
75
        } else {
76
            $arguments[] = null;
77
        }
78
        if (isset($options['locale'])) {
79
            $arguments[] = $options['locale'];
80
        } else {
81
            $arguments[] = 'en_US';
82
        }
83
    }
84
85
    protected function setRabbitMqOptionsPt2(array &$arguments, array $options)
86
    {
87
        if (isset($options['connection_timeout'])) {
88
            $arguments[] = $options['connection_timeout'];
89
        } else {
90
            $arguments[] = 3.0;
91
        }
92
        if (isset($options['read_write_timeout'])) {
93
            $arguments[] = $options['read_write_timeout'];
94
        } else {
95
            $arguments[] = 3.0;
96
        }
97
        if (isset($options['context'])) {
98
            $arguments[] = $options['context'];
99
        } else {
100
            $arguments[] = null;
101
        }
102
        if (isset($options['keepalive'])) {
103
            $arguments[] = $options['keepalive'];
104
        } else {
105
            $arguments[] = false;
106
        }
107
        if (isset($options['heartbeat'])) {
108
            $arguments[] = $options['heartbeat'];
109
        } else {
110
            $arguments[] = 0;
111
        }
112
    }
113
}
114