Completed
Push — master ( 1204eb...5574d8 )
by Matthew
05:57
created

Configuration   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 182
Duplicated Lines 17.58 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 99.22%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 32
loc 182
ccs 128
cts 129
cp 0.9922
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 49 1
A addRabbitMqOptions() 0 18 1
C addRabbitMqSslOptions() 0 32 8
A addRabbitMqExchange() 16 16 1
A addRabbitMqArgs() 16 16 1
B addRabbitMq() 0 38 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 Dtc\QueueBundle\DependencyInjection;
4
5
use Dtc\QueueBundle\Model\PriorityJobManager;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
class Configuration implements ConfigurationInterface
10
{
11
    /**
12
     * Generates the configuration tree.
13
     *
14
     * @return TreeBuilder
15
     */
16 3
    public function getConfigTreeBuilder()
17
    {
18 3
        $treeBuilder = new TreeBuilder();
19 3
        $rootNode = $treeBuilder->root('dtc_queue');
20
21
        $rootNode
22 3
            ->children()
23 3
                ->scalarNode('document_manager')
24 3
                    ->defaultValue('default')
25 3
                    ->cannotBeEmpty()
26 3
                ->end()
27 3
                ->scalarNode('entity_manager')
28 3
                    ->defaultValue('default')
29 3
                    ->cannotBeEmpty()
30 3
                ->end()
31 3
                ->scalarNode('default_manager')
32 3
                    ->defaultValue('mongodb')
33 3
                    ->cannotBeEmpty()
34 3
                ->end()
35 3
                ->scalarNode('run_manager')
36 3
                ->end()
37 3
                ->scalarNode('document_am')
38 3
                ->end()
39 3
                ->scalarNode('class_job')
40 3
                ->end()
41 3
                ->scalarNode('class_job_archive')
42 3
                ->end()
43 3
                ->scalarNode('class_job_timing')
44 3
                ->end()
45 3
                ->scalarNode('class_run')
46 3
                ->end()
47 3
                ->scalarNode('class_run_archive')
48 3
                ->end()
49 3
                ->booleanNode('record_timings')->defaultFalse()
50 3
                ->end()
51 3
                ->integerNode('priority_max')->defaultValue(255)
52 3
                ->end()
53 3
                ->enumNode('priority_direction')->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])->defaultValue(PriorityJobManager::PRIORITY_DESC)->end()
54 3
                ->arrayNode('beanstalkd')
55 3
                    ->children()
56 3
                        ->scalarNode('host')->end()
57 3
                        ->scalarNode('tube')->end()
58 3
                    ->end()
59 3
                ->end()
60 3
                ->append($this->addRabbitMq())
61 3
            ->end();
62
63 3
        return $treeBuilder;
64
    }
65
66 3
    protected function addRabbitMqOptions()
67
    {
68 3
        $treeBuilder = new TreeBuilder();
69 3
        $rootNode = $treeBuilder->root('options');
70
        $rootNode
71 3
            ->children()
72 3
                ->scalarNode('insist')->end()
73 3
                ->scalarNode('login_method')->end()
74 3
                ->scalarNode('login_response')->end()
75 3
                ->scalarNode('locale')->end()
76 3
                ->floatNode('connection_timeout')->end()
77 3
                ->floatNode('read_write_timeout')->end()
78 3
                ->booleanNode('keepalive')->end()
79 3
                ->integerNode('heartbeat')->end()
80 3
            ->end();
81
82 3
        return $rootNode;
83
    }
84
85 3
    protected function addRabbitMqSslOptions()
86
    {
87 3
        $treeBuilder = new TreeBuilder();
88 3
        $rootNode = $treeBuilder->root('ssl_options');
89
        $rootNode
90 3
            ->prototype('variable')->end()
91 3
            ->validate()
92 3
                ->ifTrue(function ($node) {
93 1
                    if (!is_array($node)) {
94
                        return true;
95
                    }
96 1
                    foreach ($node as $key => $value) {
97 1
                        if (is_array($value)) {
98 1
                            if ('peer_fingerprint' !== $key) {
99 1
                                return true;
100
                            } else {
101 1
                                foreach ($value as $key1 => $value1) {
102 1
                                    if (!is_string($key1) || !is_string($value1)) {
103 1
                                        return true;
104
                                    }
105
                                }
106
                            }
107
                        }
108
                    }
109
110 1
                    return false;
111 3
                })
112 3
                ->thenInvalid('Must be key-value pairs')
113 3
            ->end();
114
115 3
        return $rootNode;
116
    }
117
118 3 View Code Duplication
    protected function addRabbitMqExchange()
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...
119
    {
120 3
        $treeBuilder = new TreeBuilder();
121 3
        $rootNode = $treeBuilder->root('exchange_args');
122
        $rootNode
123 3
            ->addDefaultsIfNotSet()
124 3
            ->children()
125 3
                ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
126 3
                ->booleanNode('type')->defaultValue('direct')->end()
127 3
                ->booleanNode('passive')->defaultFalse()->end()
128 3
                ->booleanNode('durable')->defaultTrue()->end()
129 3
                ->booleanNode('auto_delete')->defaultFalse()->end()
130 3
            ->end();
131
132 3
        return $rootNode;
133
    }
134
135 3 View Code Duplication
    protected function addRabbitMqArgs()
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...
136
    {
137 3
        $treeBuilder = new TreeBuilder();
138 3
        $rootNode = $treeBuilder->root('queue_args');
139
        $rootNode
140 3
            ->addDefaultsIfNotSet()
141 3
            ->children()
142 3
                ->scalarNode('queue')->defaultValue('dtc_queue')->end()
143 3
                ->booleanNode('passive')->defaultFalse()->end()
144 3
                ->booleanNode('durable')->defaultTrue()->end()
145 3
                ->booleanNode('exclusive')->defaultFalse()->end()
146 3
                ->booleanNode('auto_delete')->defaultFalse()->end()
147 3
            ->end();
148
149 3
        return $rootNode;
150
    }
151
152 3
    protected function addRabbitMq()
153
    {
154 3
        $treeBuilder = new TreeBuilder();
155 3
        $rootNode = $treeBuilder->root('rabbit_mq');
156
        $rootNode
157 3
            ->children()
158 3
                ->scalarNode('host')->end()
159 3
                ->scalarNode('port')->end()
160 3
                ->scalarNode('user')->end()
161 3
                ->scalarNode('password')->end()
162 3
                ->scalarNode('vhost')->defaultValue('/')->end()
163 3
                ->booleanNode('ssl')->defaultFalse()->end()
164 3
            ->end()
165 3
            ->append($this->addRabbitMqOptions())
166 3
            ->append($this->addRabbitMqSslOptions())
167 3
            ->append($this->addRabbitMqArgs())
168 3
            ->append($this->addRabbitMqExchange())
169 3
            ->validate()->always(function ($node) {
170 1
                if (empty($node['ssl_options'])) {
171 1
                    unset($node['ssl_options']);
172
                }
173 1
                if (empty($node['options'])) {
174 1
                    unset($node['options']);
175
                }
176
177 1
                return $node;
178 3
            })->end()
179 3
            ->validate()->ifTrue(function ($node) {
180 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
181 1
                    return true;
182
                }
183
184 1
                return false;
185 3
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
186
            ->end();
187
188
        return $rootNode;
189
    }
190
}
191