Passed
Push — master ( 35a7ab...d29e83 )
by Matthew
07:21
created

RabbitMQConfiguration::addRabbitMq()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6.0014

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 32
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 44
ccs 28
cts 29
cp 0.9655
crap 6.0014
rs 8.7857
1
<?php
2
3
namespace Dtc\QueueBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
7
trait RabbitMQConfiguration
8
{
9 10
    protected function addRabbitMqOptions()
10
    {
11 10
        $treeBuilder = new TreeBuilder('options');
12
13 10
        if (method_exists($treeBuilder, 'getRootNode')) {
14 10
            $rootNode = $treeBuilder->getRootNode();
15
        } else {
16
            // BC layer for symfony/config 4.1 and older
17
            $rootNode = $treeBuilder->root('options');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

17
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('options');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
18
        }
19
20
        $rootNode
21 10
            ->children()
22 10
            ->scalarNode('insist')->end()
23 10
            ->scalarNode('login_method')->end()
24 10
            ->scalarNode('login_response')->end()
25 10
            ->scalarNode('locale')->end()
26 10
            ->floatNode('connection_timeout')->end()
27 10
            ->floatNode('read_write_timeout')->end()
28 10
            ->booleanNode('keepalive')->end()
29 10
            ->integerNode('heartbeat')->end()
30 10
            ->end();
31
32 10
        return $rootNode;
33
    }
34
35 10
    protected function addRabbitMqSslOptions()
36
    {
37 10
        $treeBuilder = new TreeBuilder('ssl_options');
38
39 10
        if (method_exists($treeBuilder, 'getRootNode')) {
40 10
            $rootNode = $treeBuilder->getRootNode();
41
        } else {
42
            // BC layer for symfony/config 4.1 and older
43
            $rootNode = $treeBuilder->root('ssl_options');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

43
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('ssl_options');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
44
        }
45
46
        $rootNode
47 10
            ->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 10
            ->validate()
49 10
            ->ifTrue(function ($node) {
50 1
                if (!is_array($node)) {
51
                    return true;
52
                }
53
54 1
                return $this->validateNode($node);
55 10
            })
56 10
            ->thenInvalid('Must be key-value pairs')
57 10
            ->end();
58
59 10
        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 10
    protected function addRabbitMqExchange()
90
    {
91 10
        $treeBuilder = new TreeBuilder('exchange_args');
92
93 10
        if (method_exists($treeBuilder, 'getRootNode')) {
94 10
            $rootNode = $treeBuilder->getRootNode();
95
        } else {
96
            // BC layer for symfony/config 4.1 and older
97
            $rootNode = $treeBuilder->root('exchange_args');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

97
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('exchange_args');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
        }
99
100
        $rootNode
101 10
            ->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 10
            ->children()
103 10
            ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
104 10
            ->booleanNode('type')->defaultValue('direct')->end()
105 10
            ->booleanNode('passive')->defaultFalse()->end()
106 10
            ->booleanNode('durable')->defaultTrue()->end()
107 10
            ->booleanNode('auto_delete')->defaultFalse()->end()
108 10
            ->end();
109
110 10
        return $rootNode;
111
    }
112
113 10
    protected function addRabbitMqArgs()
114
    {
115 10
        $treeBuilder = new TreeBuilder('queue_args');
116
117 10
        if (method_exists($treeBuilder, 'getRootNode')) {
118 10
            $rootNode = $treeBuilder->getRootNode();
119
        } else {
120
            // BC layer for symfony/config 4.1 and older
121
            $rootNode = $treeBuilder->root('queue_args');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

121
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('queue_args');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
122
        }
123
124
        $rootNode
125 10
            ->addDefaultsIfNotSet()
126 10
            ->children()
127 10
            ->scalarNode('queue')->defaultValue('dtc_queue')->end()
128 10
            ->booleanNode('passive')->defaultFalse()->end()
129 10
            ->booleanNode('durable')->defaultTrue()->end()
130 10
            ->booleanNode('exclusive')->defaultFalse()->end()
131 10
            ->booleanNode('auto_delete')->defaultFalse()->end()
132 10
            ->end();
133
134 10
        return $rootNode;
135
    }
136
137 10
    protected function addRabbitMq()
138
    {
139 10
        $treeBuilder = new TreeBuilder('rabbit_mq');
140
141 10
        if (method_exists($treeBuilder, 'getRootNode')) {
142 10
            $rootNode = $treeBuilder->getRootNode();
143
        } else {
144
            // BC layer for symfony/config 4.1 and older
145
            $rootNode = $treeBuilder->root('rabbit_mq');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

145
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('rabbit_mq');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
146
        }
147
148
        $rootNode
149 10
            ->children()
150 10
            ->scalarNode('host')->end()
151 10
            ->scalarNode('port')->end()
152 10
            ->scalarNode('user')->end()
153 10
            ->scalarNode('password')->end()
154 10
            ->scalarNode('vhost')->defaultValue('/')->end()
155 10
            ->booleanNode('ssl')->defaultFalse()->end()
156 10
            ->append($this->addRabbitMqOptions())
157 10
            ->append($this->addRabbitMqSslOptions())
158 10
            ->append($this->addRabbitMqArgs())
159 10
            ->append($this->addRabbitMqExchange())
160 10
            ->end()
161 10
            ->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 10
            })->end()
171 10
            ->validate()->ifTrue(function ($node) {
172 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
173 1
                    return true;
174
                }
175
176 1
                return false;
177 10
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
178
            ->end();
179
180
        return $rootNode;
181
    }
182
}
183