Passed
Pull Request — master (#111)
by Artem
07:44
created

RabbitMQConfiguration   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 93.07%

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 174
ccs 94
cts 101
cp 0.9307
rs 10
c 0
b 0
f 0
wmc 24

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addRabbitMqExchange() 0 22 2
A addRabbitMqOptions() 0 24 2
A addRabbitMqArgs() 0 22 2
A validateArray() 0 12 5
A addRabbitMqSslOptions() 0 25 3
A validateNode() 0 11 4
B addRabbitMq() 0 44 6
1
<?php
2
3
namespace Dtc\QueueBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
7
trait RabbitMQConfiguration
8
{
9 9
    protected function addRabbitMqOptions()
10
    {
11 9
        $treeBuilder = new TreeBuilder('options');
12
13 9
        if (method_exists($treeBuilder, 'getRootNode')) {
14 9
            $rootNode = $treeBuilder->getRootNode();
15
        } else {
16
            // BC layer for symfony/config 4.1 and older
17
            $rootNode = $treeBuilder->root('options');
18
        }
19
20
        $rootNode
21 9
            ->children()
22 9
            ->scalarNode('insist')->end()
23 9
            ->scalarNode('login_method')->end()
24 9
            ->scalarNode('login_response')->end()
25 9
            ->scalarNode('locale')->end()
26 9
            ->floatNode('connection_timeout')->end()
27 9
            ->floatNode('read_write_timeout')->end()
28 9
            ->booleanNode('keepalive')->end()
29 9
            ->integerNode('heartbeat')->end()
30 9
            ->end();
31
32 9
        return $rootNode;
33
    }
34
35 9
    protected function addRabbitMqSslOptions()
36
    {
37 9
        $treeBuilder = new TreeBuilder('ssl_options');
38
39 9
        if (method_exists($treeBuilder, 'getRootNode')) {
40 9
            $rootNode = $treeBuilder->getRootNode();
41
        } else {
42
            // BC layer for symfony/config 4.1 and older
43
            $rootNode = $treeBuilder->root('ssl_options');
44
        }
45
46
        $rootNode
47 9
            ->prototype('variable')->end()
0 ignored issues
show
Bug introduced by
The method prototype() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            ->/** @scrutinizer ignore-call */ 
48
              prototype('variable')->end()
Loading history...
48 9
            ->validate()
49 9
            ->ifTrue(function ($node) {
50 1
                if (!is_array($node)) {
51
                    return true;
52
                }
53
54 1
                return $this->validateNode($node);
55 9
            })
56 9
            ->thenInvalid('Must be key-value pairs')
57 9
            ->end();
58
59 9
        return $rootNode;
60
    }
61
62 1
    private function validateNode(array $node)
63
    {
64 1
        foreach ($node as $key => $value) {
65 1
            if (is_array($value)) {
66 1
                if ($this->validateArray($key, $value)) {
67 1
                    return true;
68
                }
69
            }
70
        }
71
72 1
        return false;
73
    }
74
75 1
    private function validateArray($key, $value)
76
    {
77 1
        if ('peer_fingerprint' !== $key) {
78 1
            return true;
79
        }
80 1
        foreach ($value as $key1 => $value1) {
81 1
            if (!is_string($key1) || !is_string($value1)) {
82
                return true;
83
            }
84
        }
85
86 1
        return false;
87
    }
88
89 9
    protected function addRabbitMqExchange()
90
    {
91 9
        $treeBuilder = new TreeBuilder('exchange_args');
92
93 9
        if (method_exists($treeBuilder, 'getRootNode')) {
94 9
            $rootNode = $treeBuilder->getRootNode();
95
        } else {
96
            // BC layer for symfony/config 4.1 and older
97
            $rootNode = $treeBuilder->root('exchange_args');
98
        }
99
100
        $rootNode
101 9
            ->addDefaultsIfNotSet()
0 ignored issues
show
Bug introduced by
The method addDefaultsIfNotSet() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
            ->/** @scrutinizer ignore-call */ 
102
              addDefaultsIfNotSet()
Loading history...
102 9
            ->children()
103 9
            ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
104 9
            ->booleanNode('type')->defaultValue('direct')->end()
105 9
            ->booleanNode('passive')->defaultFalse()->end()
106 9
            ->booleanNode('durable')->defaultTrue()->end()
107 9
            ->booleanNode('auto_delete')->defaultFalse()->end()
108 9
            ->end();
109
110 9
        return $rootNode;
111
    }
112
113 9
    protected function addRabbitMqArgs()
114
    {
115 9
        $treeBuilder = new TreeBuilder('queue_args');
116
117 9
        if (method_exists($treeBuilder, 'getRootNode')) {
118 9
            $rootNode = $treeBuilder->getRootNode();
119
        } else {
120
            // BC layer for symfony/config 4.1 and older
121
            $rootNode = $treeBuilder->root('queue_args');
122
        }
123
124
        $rootNode
125 9
            ->addDefaultsIfNotSet()
126 9
            ->children()
127 9
            ->scalarNode('queue')->defaultValue('dtc_queue')->end()
128 9
            ->booleanNode('passive')->defaultFalse()->end()
129 9
            ->booleanNode('durable')->defaultTrue()->end()
130 9
            ->booleanNode('exclusive')->defaultFalse()->end()
131 9
            ->booleanNode('auto_delete')->defaultFalse()->end()
132 9
            ->end();
133
134 9
        return $rootNode;
135
    }
136
137 9
    protected function addRabbitMq()
138
    {
139 9
        $treeBuilder = new TreeBuilder('rabbit_mq');
140
141 9
        if (method_exists($treeBuilder, 'getRootNode')) {
142 9
            $rootNode = $treeBuilder->getRootNode();
143
        } else {
144
            // BC layer for symfony/config 4.1 and older
145
            $rootNode = $treeBuilder->root('rabbit_mq');
146
        }
147
148
        $rootNode
149 9
            ->children()
150 9
            ->scalarNode('host')->end()
151 9
            ->scalarNode('port')->end()
152 9
            ->scalarNode('user')->end()
153 9
            ->scalarNode('password')->end()
154 9
            ->scalarNode('vhost')->defaultValue('/')->end()
155 9
            ->booleanNode('ssl')->defaultFalse()->end()
156 9
            ->append($this->addRabbitMqOptions())
157 9
            ->append($this->addRabbitMqSslOptions())
158 9
            ->append($this->addRabbitMqArgs())
159 9
            ->append($this->addRabbitMqExchange())
160 9
            ->end()
161 9
            ->validate()->always(function ($node) {
162 1
                if (empty($node['ssl_options'])) {
163 1
                    unset($node['ssl_options']);
164
                }
165 1
                if (empty($node['options'])) {
166 1
                    unset($node['options']);
167
                }
168
169 1
                return $node;
170 9
            })->end()
171 9
            ->validate()->ifTrue(function ($node) {
172 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
173 1
                    return true;
174
                }
175
176 1
                return false;
177 9
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
178
            ->end();
179
180
        return $rootNode;
181
    }
182
}
183