Completed
Push — master ( 5fe2ea...bba9f0 )
by Matthew
13:23
created

Configuration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 205
Duplicated Lines 15.61 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 32
loc 205
ccs 0
cts 177
cp 0
rs 10
c 0
b 0
f 0

7 Methods

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