Completed
Pull Request — master (#30)
by Matthew
23:29 queued 08:10
created

Configuration::addSncRedis()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 10
Ratio 40 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
dl 10
loc 25
ccs 18
cts 18
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 1
nop 0
crap 5
1
<?php
2
3
namespace Dtc\QueueBundle\DependencyInjection;
4
5
use Dtc\QueueBundle\Manager\PriorityJobManager;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Symfony\Component\HttpKernel\Kernel;
9
10
class Configuration implements ConfigurationInterface
11
{
12
    /**
13
     * Generates the configuration tree.
14
     *
15
     * @return TreeBuilder
16
     */
17 8
    public function getConfigTreeBuilder()
18
    {
19 8
        $treeBuilder = new TreeBuilder();
20 8
        $rootNode = $treeBuilder->root('dtc_queue');
21
22
        $node = $rootNode
23 8
            ->children()
24 8
                ->append($this->addOrm())
25 8
                ->append($this->addOdm())
26 8
                ->append($this->addManager())
27 8
                ->append($this->addTimings())
28 8
                ->append($this->addBeanstalkd())
29 8
                ->append($this->addRabbitMq())
30 8
                ->append($this->addRedis())
31 8
                ->append($this->addAdmin())
32 8
                ->append($this->addClasses())
33 8
                ->append($this->addPriority())
34 8
                ->append($this->addRetry());
35
36 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'document_manager', 'The "%node% option is deprecated, Use "odm: { document_manager: ... }" instead.');
37 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'entity_manager', 'The "%node% option is deprecated, Use "odm: { entity_manager: ... }" instead.');
38 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'default_manager', 'The "%node% option is deprecated, Use "manager: { job: ... }" instead.');
39 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'run_manager', 'The "%node% option is deprecated, Use "manager: { run: ... }" instead.');
40 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'job_timing_manager', 'The "%node% option is deprecated, Use "manager: { job_timing: ... }" instead.');
41 8
        $node = $this->setDeprecatedNode($node, 'booleanNode', 'record_timings', 'The "%node% option is deprecated, Use "timings: { record: ... }" instead.');
42 8
        $node = $this->setDeprecatedNode($node, 'integerNode', 'record_timings_timezone_offset', 'The "%node% option is deprecated, Use "record: { timezone_offset: ... }" instead.');
43 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job', 'The "%node% option is deprecated, Use "class: { job: ... }" instead.');
44 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_archive', 'The "%node% option is deprecated, Use "class: { job_archive: ... }" instead.');
45 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run', 'The "%node% option is deprecated, Use "class: { run: ... }" instead.');
46 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run_archive', 'The "%node% option is deprecated, Use "class: { run_archive: ... }" instead.');
47 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_timing', 'The "%node% option is deprecated, Use "class: { job_timing: ... }" instead.');
48 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_max', 'The "%node% option is deprecated, Use "priority: { max: ... }" instead.');
49 8
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_direction', 'The "%node% option is deprecated, Use "priority: { direction: ... }" instead.');
50 8
        $node->end();
51
52 8
        return $treeBuilder;
53
    }
54
55 8
    public function setDeprecatedNode($node, $type, $name, $deprecatedMessage)
56
    {
57 8
        $node = $node->$type($name);
58
59 8
        if (Kernel::VERSION_ID >= 30400) {
60 8
            $node = $node->setDeprecated($deprecatedMessage);
61
        }
62
63 8
        return $node->end();
64
    }
65
66 8
    protected function addTimings()
67
    {
68 8
        $treeBuilder = new TreeBuilder();
69 8
        $rootNode = $treeBuilder->root('timings');
70
        $rootNode
71 8
            ->addDefaultsIfNotSet()
72 8
            ->children()
73 8
                ->booleanNode('record')
74 8
                    ->info('Set this to true to record timings (used on the Trends page)')
75 8
                    ->defaultFalse()
76 8
                ->end()
77 8
                ->floatNode('timezone_offset')
78 8
                    ->defaultValue(0)
79 8
                    ->info('Set this some integer offset from GMT in case your database is not storing things in the proper timezone and the dates look off on the Trends page')
80 8
                    ->max(24)
81 8
                    ->min(-24)
82 8
                ->end()
83 8
            ->end();
84
85 8
        return $rootNode;
86
    }
87
88 8 View Code Duplication
    protected function addOrm()
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...
89
    {
90 8
        $treeBuilder = new TreeBuilder();
91 8
        $rootNode = $treeBuilder->root('orm');
92
        $rootNode
93 8
            ->addDefaultsIfNotSet()
94 8
            ->children()
95 8
                ->scalarNode('entity_manager')
96 8
                    ->info('This only needs to be set if orm is used for any of the managers, and you do not want to use the default entity manager')
97 8
                    ->defaultValue('default')
98 8
                    ->cannotBeEmpty()
99 8
                ->end()
100 8
            ->end();
101
102 8
        return $rootNode;
103
    }
104
105 8 View Code Duplication
    protected function addOdm()
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...
106
    {
107 8
        $treeBuilder = new TreeBuilder();
108 8
        $rootNode = $treeBuilder->root('odm');
109
        $rootNode
110 8
            ->addDefaultsIfNotSet()
111 8
            ->children()
112 8
                ->scalarNode('document_manager')
113 8
                    ->info('This only needs to be set if odm is used for any of the managers, and you do not want to use the default document manager')
114 8
                    ->defaultValue('default')
115 8
                    ->cannotBeEmpty()
116 8
                ->end()
117 8
            ->end();
118
119 8
        return $rootNode;
120
    }
121
122 8
    protected function addManager()
123
    {
124 8
        $treeBuilder = new TreeBuilder();
125 8
        $rootNode = $treeBuilder->root('manager');
126
        $rootNode
127 8
            ->addDefaultsIfNotSet()
128 8
            ->children()
129 8
                ->scalarNode('job')
130 8
                    ->defaultValue('odm')
131 8
                    ->info('This can be [odm|orm|beanstalkd|rabbit_mq|redis|(your-own-custom-one)]')
132 8
                    ->cannotBeEmpty()
133 8
                ->end()
134 8
                ->scalarNode('run')->end()
135 8
                ->scalarNode('job_timing')->end()
136 8
            ->end();
137
138 8
        return $rootNode;
139
    }
140
141 8
    protected function addBeanstalkd()
142
    {
143 8
        $treeBuilder = new TreeBuilder();
144 8
        $rootNode = $treeBuilder->root('beanstalkd');
145
        $rootNode
146 8
            ->children()
147 8
                ->scalarNode('host')->end()
148 8
                ->scalarNode('tube')->end()
149 8
            ->end();
150
151 8
        return $rootNode;
152
    }
153
154 8
    protected function addRetry()
155
    {
156 8
        $treeBuilder = new TreeBuilder();
157 8
        $rootNode = $treeBuilder->root('retry');
158
        $rootNode
159 8
            ->addDefaultsIfNotSet()
160 8
            ->children()
161 8
                ->arrayNode('max')
162 8
                    ->addDefaultsIfNotSet()
163 8
                    ->children()
164 8
                        ->integerNode('retries')
165 8
                            ->info('This the maximum total number of retries of any type.')
166 8
                            ->defaultValue(3)
167 8
                        ->end()
168 8
                        ->integerNode('failures')
169 8
                            ->info('This the maximum total number of failures before a job is marked as hitting the maximum failures.')
170 8
                            ->defaultValue(1)
171 8
                        ->end()
172 8
                        ->integerNode('exceptions')
173 8
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
174 8
                            ->defaultValue(2)
175 8
                        ->end()
176 8
                        ->integerNode('stalls')
177 8
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
178 8
                            ->defaultValue(2)
179 8
                        ->end()
180 8
                    ->end()
181 8
                ->end()
182 8
                ->arrayNode('auto')
183 8
                    ->addDefaultsIfNotSet()
184 8
                    ->children()
185 8
                        ->booleanNode('failure')
186 8
                            ->info('Instantly re-queue the job on failure.')
187 8
                            ->defaultTrue()
188 8
                        ->end()
189 8
                        ->booleanNode('exception')
190 8
                            ->info('Instantly re-queue the job on exception.')
191 8
                            ->defaultFalse()
192 8
                        ->end()
193 8
                    ->end()
194 8
                ->end()
195 8
            ->end();
196
197 8
        return $rootNode;
198
    }
199
200 8
    protected function addPriority()
201
    {
202 8
        $treeBuilder = new TreeBuilder();
203 8
        $rootNode = $treeBuilder->root('priority');
204
        $rootNode
205 8
            ->addDefaultsIfNotSet()
206 8
            ->children()
207 8
                ->integerNode('max')
208 8
                    ->defaultValue(255)
209 8
                    ->info('Maximum priority value.')
210 8
                    ->min(1)
211 8
                ->end()
212 8
                ->enumNode('direction')
213 8
                    ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])
214 8
                    ->info('Whether 1 is high priority or low prioirty.  '.PriorityJobManager::PRIORITY_ASC.' means 1 is low, '.PriorityJobManager::PRIORITY_DESC.' means 1 is high.')
215 8
                    ->defaultValue(PriorityJobManager::PRIORITY_DESC)
216 8
                ->end()
217 8
            ->end();
218
219 8
        return $rootNode;
220
    }
221
222 8
    protected function addClasses()
223
    {
224 8
        $treeBuilder = new TreeBuilder();
225 8
        $rootNode = $treeBuilder->root('class');
226
        $rootNode
227 8
            ->children()
228 8
                ->scalarNode('job')
229 8
                    ->info('If you want to override the Job class, put the class name here.')->end()
230 8
                ->scalarNode('job_archive')
231 8
                    ->info('If you want to override the JobArchive class, put the class name here.')->end()
232 8
                ->scalarNode('job_timing')
233 8
                    ->info('If you want to override the JobTiming class, put the class name here.')->end()
234 8
                ->scalarNode('run')
235 8
                    ->info('If you want to override the Run class, put the class name here.')->end()
236 8
                ->scalarNode('run_archive')
237 8
                    ->info('If you want to override the RunArchive class, put the class name here.')->end()
238 8
            ->end();
239
240 8
        return $rootNode;
241
    }
242
243 8 View Code Duplication
    protected function addAdmin()
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...
244
    {
245 8
        $treeBuilder = new TreeBuilder();
246 8
        $rootNode = $treeBuilder->root('admin');
247
        $rootNode
248 8
            ->addDefaultsIfNotSet()
249 8
            ->children()
250 8
                ->scalarNode('chartjs')
251 8
                    ->defaultValue('https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js')
252 8
                    ->info('This can be changed to say a locally hosted path or url.')->end()
253 8
                ->end()
254 8
            ->end();
255
256 8
        return $rootNode;
257
    }
258
259 8
    protected function addSncRedis()
260
    {
261 8
        $treeBuilder = new TreeBuilder();
262 8
        $rootNode = $treeBuilder->root('snc_redis');
263
        $rootNode
264 8
            ->children()
265 8
                ->enumNode('type')
266 8
                    ->values(['predis', 'phpredis'])
267 8
                    ->defaultNull()->end()
268 8
                ->scalarNode('alias')
269 8
                    ->defaultNull()->end()
270 8
            ->end()
271 8 View Code Duplication
            ->validate()->ifTrue(function($node) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
272 1
                if (isset($node['type']) && !isset($node['alias'])) {
273 1
                    return true;
274
                }
275 1
                if (isset($node['alias']) && !isset($node['type'])) {
276 1
                    return true;
277
                }
278
279 1
                return false;
280 8
            })->thenInvalid('if alias or type is set, then both must be set')->end();
281
282 8
        return $rootNode;
283
    }
284
285 8
    protected function addPredis()
286
    {
287 8
        $treeBuilder = new TreeBuilder();
288 8
        $rootNode = $treeBuilder->root('predis');
289
        $rootNode
290 8
            ->children()
291 8
                ->scalarNode('dsn')->defaultNull()->end()
292 8
                ->append($this->addPredisArgs())
293 8
            ->end()
294 8
            ->validate()->ifTrue(function($node) {
295 1
                if (isset($node['dsn']) && (isset($node['connection_parameters']['host']) || isset($node['connection_parameters']['port']))) {
296
                    return true;
297
                }
298
299 1
                return false;
300 8
            })->thenInvalid('if dsn is set, do not use connection_parameters for predis (and vice-versa)')->end();
301
302 8
        return $rootNode;
303
    }
304
305 8
    protected function addRedis()
306
    {
307 8
        $treeBuilder = new TreeBuilder();
308 8
        $rootNode = $treeBuilder->root('redis');
309
        $rootNode
310 8
            ->addDefaultsIfNotSet()
311 8
            ->children()
312 8
                ->scalarNode('prefix')->defaultValue('dtc_queue_')->end()
313 8
                ->append($this->addSncRedis())
314 8
                ->append($this->addPredis())
315 8
                ->append($this->addPhpRedisArgs())
316 8
            ->end()
317 8
            ->validate()->ifTrue(function($node) {
318 4
                if ((isset($node['predis']['dsn']) || isset($node['predis']['connection_parameters']['host'])) &&
319 4
                    (isset($node['snc_redis']['type']) || isset($node['phpredis']['host']))) {
320
                    return true;
321
                }
322 4
                if (isset($node['snc_redis']['type']) &&
323 4
                    isset($node['phpredis']['host'])) {
324
                    return true;
325
                }
326
327 4
                return false;
328 8
            })->thenInvalid('only one of [snc_redis | predis | phpredis] should be set')->end();
329
330 8
        return $rootNode;
331
    }
332
333 8
    protected function addPhpRedisArgs()
334
    {
335 8
        $treeBuilder = new TreeBuilder();
336 8
        $rootNode = $treeBuilder->root('phpredis');
337
        $rootNode
338 8
            ->addDefaultsIfNotSet()
339 8
            ->children()
340 8
                ->scalarNode('host')->end()
341 8
                ->integerNode('port')->defaultValue(6379)->end()
342 8
                ->floatNode('timeout')->defaultValue(0)->end()
343 8
                ->integerNode('retry_interval')->defaultNull()->end()
344 8
                ->floatNode('read_timeout')->defaultValue(0)->end()
345 8
                ->scalarNode('auth')->end()
346 8
            ->end()
347 8
            ->validate()->ifTrue(function($node) {
348 2
                if (!empty($node) && !isset($node['host'])) {
349
                    return true;
350
                }
351
352 2
                return false;
353 8
            })->thenInvalid('phpredis host should be set')->end();
354
355 8
        return $rootNode;
356
    }
357
358 8
    protected function addPredisArgs()
359
    {
360 8
        $treeBuilder = new TreeBuilder();
361 8
        $rootNode = $treeBuilder->root('connection_parameters');
362
        $rootNode
363 8
            ->addDefaultsIfNotSet()
364 8
            ->children()
365 8
                ->scalarNode('scheme')->defaultValue('tcp')->end()
366 8
                ->scalarNode('host')->defaultNull()->end()
367 8
                ->integerNode('port')->defaultNull()->end()
368 8
                ->scalarNode('path')->defaultNull()->end()
369 8
                ->scalarNode('database')->defaultNull()->end()
370 8
                ->scalarNode('password')->defaultNull()->end()
371 8
                ->booleanNode('async')->defaultFalse()->end()
372 8
                ->booleanNode('persistent')->defaultFalse()->end()
373 8
                ->floatNode('timeout')->defaultValue(5.0)->end()
374 8
                ->floatNode('read_write_timeout')->defaultNull()->end()
375 8
                ->scalarNode('alias')->defaultNull()->end()
376 8
                ->integerNode('weight')->defaultNull()->end()
377 8
                ->booleanNode('iterable_multibulk')->defaultFalse()->end()
378 8
                ->booleanNode('throw_errors')->defaultTrue()->end()
379 8
            ->end()
380 8 View Code Duplication
            ->validate()->ifTrue(function($node) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
381 1
                if (isset($node['host']) && !isset($node['port'])) {
382
                    return true;
383
                }
384 1
                if (isset($node['port']) && !isset($node['host'])) {
385
                    return true;
386
                }
387
388 1
                return false;
389 8
            })->thenInvalid('preids connection_parameters host and port should both be set')->end();
390
391 8
        return $rootNode;
392
    }
393
394 8
    protected function addRabbitMqOptions()
395
    {
396 8
        $treeBuilder = new TreeBuilder();
397 8
        $rootNode = $treeBuilder->root('options');
398
        $rootNode
399 8
            ->children()
400 8
                ->scalarNode('insist')->end()
401 8
                ->scalarNode('login_method')->end()
402 8
                ->scalarNode('login_response')->end()
403 8
                ->scalarNode('locale')->end()
404 8
                ->floatNode('connection_timeout')->end()
405 8
                ->floatNode('read_write_timeout')->end()
406 8
                ->booleanNode('keepalive')->end()
407 8
                ->integerNode('heartbeat')->end()
408 8
            ->end();
409
410 8
        return $rootNode;
411
    }
412
413 8
    protected function addRabbitMqSslOptions()
414
    {
415 8
        $treeBuilder = new TreeBuilder();
416 8
        $rootNode = $treeBuilder->root('ssl_options');
417
        $rootNode
418 8
            ->prototype('variable')->end()
419 8
            ->validate()
420 8
                ->ifTrue(function($node) {
421 1
                    if (!is_array($node)) {
422
                        return true;
423
                    }
424 1
                    foreach ($node as $key => $value) {
425 1
                        if (is_array($value)) {
426 1
                            if ('peer_fingerprint' !== $key) {
427 1
                                return true;
428
                            } else {
429 1
                                foreach ($value as $key1 => $value1) {
430 1
                                    if (!is_string($key1) || !is_string($value1)) {
431 1
                                        return true;
432
                                    }
433
                                }
434
                            }
435
                        }
436
                    }
437
438 1
                    return false;
439 8
                })
440 8
                ->thenInvalid('Must be key-value pairs')
441 8
            ->end();
442
443 8
        return $rootNode;
444
    }
445
446 8 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...
447
    {
448 8
        $treeBuilder = new TreeBuilder();
449 8
        $rootNode = $treeBuilder->root('exchange_args');
450
        $rootNode
451 8
            ->addDefaultsIfNotSet()
452 8
            ->children()
453 8
                ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
454 8
                ->booleanNode('type')->defaultValue('direct')->end()
455 8
                ->booleanNode('passive')->defaultFalse()->end()
456 8
                ->booleanNode('durable')->defaultTrue()->end()
457 8
                ->booleanNode('auto_delete')->defaultFalse()->end()
458 8
            ->end();
459
460 8
        return $rootNode;
461
    }
462
463 8 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...
464
    {
465 8
        $treeBuilder = new TreeBuilder();
466 8
        $rootNode = $treeBuilder->root('queue_args');
467
        $rootNode
468 8
            ->addDefaultsIfNotSet()
469 8
            ->children()
470 8
                ->scalarNode('queue')->defaultValue('dtc_queue')->end()
471 8
                ->booleanNode('passive')->defaultFalse()->end()
472 8
                ->booleanNode('durable')->defaultTrue()->end()
473 8
                ->booleanNode('exclusive')->defaultFalse()->end()
474 8
                ->booleanNode('auto_delete')->defaultFalse()->end()
475 8
            ->end();
476
477 8
        return $rootNode;
478
    }
479
480 8
    protected function addRabbitMq()
481
    {
482 8
        $treeBuilder = new TreeBuilder();
483 8
        $rootNode = $treeBuilder->root('rabbit_mq');
484
        $rootNode
485 8
            ->children()
486 8
                ->scalarNode('host')->end()
487 8
                ->scalarNode('port')->end()
488 8
                ->scalarNode('user')->end()
489 8
                ->scalarNode('password')->end()
490 8
                ->scalarNode('vhost')->defaultValue('/')->end()
491 8
                ->booleanNode('ssl')->defaultFalse()->end()
492 8
                ->append($this->addRabbitMqOptions())
493 8
                ->append($this->addRabbitMqSslOptions())
494 8
                ->append($this->addRabbitMqArgs())
495 8
                ->append($this->addRabbitMqExchange())
496 8
            ->end()
497 8
            ->validate()->always(function($node) {
498 1
                if (empty($node['ssl_options'])) {
499 1
                    unset($node['ssl_options']);
500
                }
501 1
                if (empty($node['options'])) {
502 1
                    unset($node['options']);
503
                }
504
505 1
                return $node;
506 8
            })->end()
507 8
            ->validate()->ifTrue(function ($node) {
508 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
509 1
                    return true;
510
                }
511
512 1
                return false;
513 8
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
514
        ->end();
515
516
        return $rootNode;
517
    }
518
}
519