Completed
Push — master ( 825767...a283c3 )
by Olivier
02:15
created

Configuration   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 211
Duplicated Lines 15.17 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 99.23%

Importance

Changes 8
Bugs 0 Features 4
Metric Value
wmc 24
c 8
b 0
f 4
lcom 1
cbo 5
dl 32
loc 211
rs 10
ccs 129
cts 130
cp 0.9923

7 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 33 1
A getPermissionsConfiguration() 15 15 1
A getExchangesConfiguration() 17 17 1
B getQueuesConfiguration() 0 33 1
C getModulusValidation() 0 29 8
C appendNameNormalization() 0 22 7
B getQueueArgumentsConfiguration() 0 28 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\DependencyInjection;
4
5
use Ola\RabbitMqAdminToolkitBundle\Manager\QueueManager;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 7
    public function getConfigTreeBuilder()
17
    {
18 7
        $treeBuilder = new TreeBuilder();
19 7
        $rootNode = $treeBuilder->root('ola_rabbit_mq_admin_toolkit');
20
21
        $rootNode
22 7
            ->addDefaultsIfNotSet()
23 7
            ->children()
24 7
                ->scalarNode('default_vhost')->defaultValue('default')->end()
25 7
                ->scalarNode('delete_allowed')->defaultFalse()->end()
26 7
                ->scalarNode('silent_failure')->defaultFalse()->end()
27 7
                ->arrayNode('connections')
28 7
                    ->useAttributeAsKey('identifier')
29 7
                    ->prototype('scalar')->end()
30 7
                ->end()
31 7
                ->arrayNode('vhosts')
32 7
                    ->useAttributeAsKey('identifier')
33 7
                    ->canBeUnset(true)
34 7
                    ->prototype('array')
35 7
                        ->children()
36 7
                            ->scalarNode('name')->end()
37 7
                            ->scalarNode('connection')->defaultValue('default')->end()
38 7
                            ->scalarNode('delete_allowed')->end()
39 7
                            ->append($this->getPermissionsConfiguration())
40 7
                            ->append($this->getExchangesConfiguration())
41 7
                            ->append($this->getQueuesConfiguration())
42 7
                        ->end()
43 7
                    ->end()
44 7
                ->end()
45 7
            ->end()
46
        ;
47 7
        return $treeBuilder;
48
    }
49
50
    /**
51
     * @return NodeDefinition
52
     */
53 7 View Code Duplication
    private function getPermissionsConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 7
        $node = new ArrayNodeDefinition('permissions');
56
57
        return $node
58 7
            ->useAttributeAsKey('identifier')
59 7
            ->canBeUnset(true)
60 7
            ->prototype('array')
61 7
                ->children()
62 7
                    ->scalarNode('configure')->defaultValue('.*')->end()
63 7
                    ->scalarNode('read')->defaultValue('.*')->end()
64 7
                    ->scalarNode('write')->defaultValue('.*')->end()
65 7
                ->end()
66 7
            ->end();
67
    }
68
69
    /**
70
     * @return NodeDefinition
71
     */
72 7 View Code Duplication
    private function getExchangesConfiguration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74 7
        $node = new ArrayNodeDefinition('exchanges');
75
76 7
        $this->appendNameNormalization($node);
77
78
        return $node
79 7
            ->useAttributeAsKey('identifier')
80 7
            ->canBeUnset(true)
81 7
            ->prototype('array')
82 7
                ->children()
83 7
                    ->scalarNode('name')->end()
84 7
                    ->scalarNode('type')->defaultValue('topic')->end()
85 7
                    ->scalarNode('durable')->defaultTrue()->end()
86 7
                ->end()
87 7
            ->end();
88
    }
89
90
    /**
91
     * @return NodeDefinition
92
     */
93 7
    private function getQueuesConfiguration()
94
    {
95 7
        $modulusValidation = $this->getModulusValidation();
96
97 7
        $node = new ArrayNodeDefinition('queues');
98
99 7
        $this->appendNameNormalization($node);
100
101
        return $node
102 7
            ->useAttributeAsKey('identifier')
103 7
            ->validate()
104 7
                ->ifTrue($modulusValidation)
105 7
                ->thenInvalid('If queue.modulus is defined queue.name & at least one associated routing_key should contain a {modulus} part')
106 7
            ->end()
107 7
            ->prototype('array')
108 7
                ->canBeUnset(true)
109 7
                ->children()
110 7
                    ->scalarNode('name')->end()
111 7
                    ->scalarNode('durable')->defaultTrue()->end()
112 7
                    ->scalarNode('modulus')->defaultNull()->end()
113 7
                    ->append($this->getQueueArgumentsConfiguration())
114 7
                    ->arrayNode('bindings')
115 7
                        ->performNoDeepMerging()
116 7
                        ->prototype('array')
117 7
                            ->children()
118 7
                                ->scalarNode('exchange')->end()
119 7
                                ->scalarNode('routing_key')->end()
120 7
                            ->end()
121 7
                        ->end()
122 7
                    ->end()
123 7
                ->end()
124 7
            ->end();
125
    }
126
127
    /**
128
     * Get modulus validation closure
129
     *
130
     * @return \Closure
131
     */
132 7
    private function getModulusValidation()
133
    {
134
        return function($queues) {
135
136
            $hasModulus = function($string) {
137 4
                return strpos($string, QueueManager::MODULUS_PLACEHOLDER) !== false;
138 7
            };
139
140 7
            foreach ($queues as $name => $queue) {
141 7
                if (isset($queue['modulus']) && is_int($queue['modulus'])) {
142
143 4
                    if (!$hasModulus($queue['name'])) {
144 1
                        return true;
145
                    }
146
147 3
                    $bindingsHaveModulus = false;
148 3
                    foreach ($queue['bindings'] as $binding) {
149 3
                        if ($hasModulus($binding['routing_key'])) {
150 1
                            $bindingsHaveModulus = true;
151 1
                        }
152 3
                    }
153
154 3
                    if (!$bindingsHaveModulus) {
155 2
                        return true;
156
                    }
157 1
                }
158 4
            }
159 7
        };
160
    }
161
162
    /**
163
     * @param NodeDefinition $node
164
     *
165
     * @return NodeDefinition
166
     */
167 7
    private function appendNameNormalization(NodeDefinition $node)
168
    {
169
        return $node
170 7
            ->beforeNormalization()
171
                ->ifTrue(function($array) {
172 7
                    foreach ($array as $item) {
173 7
                        if (false !== $item && !isset($item['name'])) {
174 7
                            return true;
175
                        }
176 3
                    }
177 7
                })
178
                ->then(function($array) {
179 7
                    foreach ($array as $name => $item) {
180 7
                        if (false !== $item && !isset($item['name'])) {
181 7
                            $array[$name]['name'] = $name;
182 7
                        }
183 7
                    }
184
185 7
                    return $array;
186 7
                })
187 7
            ->end();
188
    }
189
190
    /**
191
     * @return NodeDefinition
192
     */
193 7
    private function getQueueArgumentsConfiguration()
194
    {
195 7
        $node = new ArrayNodeDefinition('arguments');
196
197
        return $node
198
            // We have to un-normalize arguments keys, as this is not configurable in SF 2.1
199 7
            ->beforeNormalization()
200
                ->ifTrue(function($arguments) {
201 1
                    foreach ($arguments as $k => $v) {
202
                        //Un-normalize keys
203 1
                        if (false !== strpos($k, '_')) {
204 1
                            return true;
205
                        }
206
                    }
207 7
                })
208 7
                ->then(function($arguments) {
209 1
                    foreach ($arguments as $k => $v) {
210 1
                        if (false !== strpos($k, '_')) {
211 1
                            $arguments[str_replace('_', '-', $k)] = $v;
212 1
                            unset($arguments[$k]);
213 1
                        }
214 1
                    }
215
216 1
                    return $arguments;
217 7
                })
218 7
            ->end()
219 7
            ->prototype('scalar')->end();
220
    }
221
}
222