Completed
Push — master ( 294ea0...cf2de2 )
by Matthew
41:13
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 50
cts 50
cp 1
rs 9.7692
c 0
b 0
f 0
cc 1
eloc 51
nc 1
nop 0
crap 1

How to fix   Long Method   

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