Completed
Push — master ( e02161...971421 )
by Matthew
12:50
created

Configuration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 202
Duplicated Lines 15.84 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

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

7 Methods

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