Completed
Push — master ( 82b02f...a3991b )
by Olivier
03:49
created

Configuration::getConfigTreeBuilder()   D

Complexity

Conditions 10
Paths 1

Size

Total Lines 96
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 79
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 96
rs 4.9494
ccs 79
cts 79
cp 1
cc 10
eloc 80
nc 1
nop 0
crap 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\DependencyInjection;
4
5
use Ola\RabbitMqAdminToolkitBundle\Manager\QueueManager;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
class Configuration implements ConfigurationInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 4
    public function getConfigTreeBuilder()
15
    {
16 4
        $treeBuilder = new TreeBuilder();
17 4
        $rootNode = $treeBuilder->root('ola_rabbit_mq_admin_toolkit');
18
19 4
        $modulusValidation = function($v) {
20 4
            foreach ($v as $name => $queue) {
21 4
                if (isset($queue['modulus']) && is_int($queue['modulus'])) {
22 3
                    if (strpos($name, QueueManager::MODULUS_PLACEHOLDER) === false) {
23 2
                        if (isset($queue['name'])) {
24 1
                            if (strpos($queue['name'], QueueManager::MODULUS_PLACEHOLDER) === false) {
25 1
                                return true;
26
                            }
27
                        } else {
28 1
                            return true;
29
                        }
30
                    }
31
32 2
                    $hasModulus = false;
33 2
                    foreach ($queue['bindings'] as $binding) {
34 2
                        if (strpos($binding['routing_key'], QueueManager::MODULUS_PLACEHOLDER) !== false) {
35 2
                            $hasModulus = true;
36
                        }
37
                    }
38
39 2
                    if (!$hasModulus) {
40 3
                        return true;
41
                    }
42
                }
43
            }
44 4
        };
45
46
        $rootNode
47 4
            ->addDefaultsIfNotSet()
48 4
            ->children()
49 4
                ->scalarNode('default_vhost')->defaultValue('default')->end()
50 4
                ->scalarNode('delete_allowed')->defaultFalse()->end()
51 4
                ->scalarNode('silent_failure')->defaultFalse()->end()
52 4
                ->arrayNode('connections')
53 4
                    ->prototype('scalar')
54 4
                    ->end()
55 4
                ->end()
56 4
                ->arrayNode('vhosts')
57 4
                    ->prototype('array')
58 4
                        ->children()
59 4
                            ->scalarNode('name')->end()
60 4
                            ->scalarNode('connection')->defaultValue('default')->end()
61 4
                            ->scalarNode('delete_allowed')->end()
62 4
                            ->arrayNode('permissions')
63 4
                                ->prototype('array')
64 4
                                    ->children()
65 4
                                        ->scalarNode('configure')->defaultValue('.*')->end()
66 4
                                        ->scalarNode('read')->defaultValue('.*')->end()
67 4
                                        ->scalarNode('write')->defaultValue('.*')->end()
68 4
                                    ->end()
69 4
                                ->end()
70 4
                            ->end()
71 4
                            ->arrayNode('exchanges')
72 4
                                ->prototype('array')
73 4
                                    ->children()
74 4
                                        ->scalarNode('name')->end()
75 4
                                        ->scalarNode('type')->defaultValue('topic')->end()
76 4
                                        ->scalarNode('durable')->defaultTrue()->end()
77 4
                                    ->end()
78 4
                                ->end()
79 4
                            ->end()
80 4
                            ->arrayNode('queues')
81 4
                                ->validate()
82 4
                                    ->ifTrue($modulusValidation)
83 4
                                    ->thenInvalid('If queue.modulus is defined queue.name & at least one associated routing_key should contain a {modulus} part')
84 4
                                ->end()
85 4
                                ->prototype('array')
86 4
                                    ->children()
87 4
                                        ->scalarNode('name')->end()
88 4
                                        ->scalarNode('durable')->defaultTrue()->end()
89 4
                                        ->scalarNode('modulus')
90 4
                                            ->defaultNull()
91 4
                                        ->end()
92 4
                                        ->arrayNode('bindings')
93 4
                                            ->prototype('array')
94 4
                                                ->children()
95 4
                                                    ->scalarNode('exchange')->end()
96 4
                                                    ->scalarNode('routing_key')->end()
97 4
                                                ->end()
98 4
                                            ->end()
99 4
                                        ->end()
100 4
                                    ->end()
101 4
                                ->end()
102 4
                            ->end()
103 4
                        ->end()
104 4
                    ->end()
105 4
                ->end()
106 4
            ->end()
107
        ;
108 4
        return $treeBuilder;
109
    }
110
}
111