Passed
Pull Request — master (#57)
by Matthew
07:32
created

Configuration::addRabbitMqOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
ccs 14
cts 14
cp 1
cc 1
eloc 14
nc 1
nop 0
crap 1
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 9
    public function getConfigTreeBuilder()
18
    {
19 9
        $treeBuilder = new TreeBuilder();
20 9
        $rootNode = $treeBuilder->root('dtc_queue');
21
22
        $node = $rootNode
23 9
            ->children()
24 9
                ->append($this->addOrm())
25 9
                ->append($this->addOdm())
26 9
                ->append($this->addManager())
27 9
                ->append($this->addTimings())
28 9
                ->append($this->addBeanstalkd())
29 9
                ->append($this->addRabbitMq())
30 9
                ->append($this->addRedis())
31 9
                ->append($this->addAdmin())
32 9
                ->append($this->addClasses())
33 9
                ->append($this->addPriority())
34 9
                ->append($this->addRetry());
35
36 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'document_manager', 'The "%node% option is deprecated, Use "odm: { document_manager: ... }" instead.');
37 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'entity_manager', 'The "%node% option is deprecated, Use "orm: { entity_manager: ... }" instead.');
38 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'default_manager', 'The "%node% option is deprecated, Use "manager: { job: ... }" instead.');
39 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'run_manager', 'The "%node% option is deprecated, Use "manager: { run: ... }" instead.');
40 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'job_timing_manager', 'The "%node% option is deprecated, Use "manager: { job_timing: ... }" instead.');
41 9
        $node = $this->setDeprecatedNode($node, 'booleanNode', 'record_timings', 'The "%node% option is deprecated, Use "timings: { record: ... }" instead.');
42 9
        $node = $this->setDeprecatedNode($node, 'floatNode', 'record_timings_timezone_offset', 'The "%node% option is deprecated, Use "record: { timezone_offset: ... }" instead.');
43 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job', 'The "%node% option is deprecated, Use "class: { job: ... }" instead.');
44 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_archive', 'The "%node% option is deprecated, Use "class: { job_archive: ... }" instead.');
45 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run', 'The "%node% option is deprecated, Use "class: { run: ... }" instead.');
46 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run_archive', 'The "%node% option is deprecated, Use "class: { run_archive: ... }" instead.');
47 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_timing', 'The "%node% option is deprecated, Use "class: { job_timing: ... }" instead.');
48 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_max', 'The "%node% option is deprecated, Use "priority: { max: ... }" instead.');
49 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_direction', 'The "%node% option is deprecated, Use "priority: { direction: ... }" instead.');
50 9
        $node->end();
51
52 9
        return $treeBuilder;
53
    }
54
55 9
    public function setDeprecatedNode($node, $type, $name, $deprecatedMessage)
56
    {
57 9
        $node = $node->$type($name);
58
59 9
        if (Kernel::VERSION_ID >= 30400) {
60 9
            $node = $node->setDeprecated($deprecatedMessage);
61
        }
62
63 9
        return $node->end();
64
    }
65
66 9
    protected function addTimings()
67
    {
68 9
        $treeBuilder = new TreeBuilder();
69 9
        $rootNode = $treeBuilder->root('timings');
70
        $rootNode
71 9
            ->addDefaultsIfNotSet()
0 ignored issues
show
Bug introduced by
The method addDefaultsIfNotSet() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            ->/** @scrutinizer ignore-call */ 
72
              addDefaultsIfNotSet()
Loading history...
72 9
            ->children()
73 9
                ->booleanNode('record')
74 9
                    ->info('Set this to true to record timings (used on the Trends page)')
75 9
                    ->defaultFalse()
76 9
                ->end()
77 9
                ->floatNode('timezone_offset')
78 9
                    ->defaultValue(0)
79 9
                    ->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 9
                    ->max(24)
81 9
                    ->min(-24)
82 9
                ->end()
83 9
            ->end();
84
85 9
        return $rootNode;
86
    }
87
88 9
    protected function addOrm()
89
    {
90 9
        $treeBuilder = new TreeBuilder();
91 9
        $rootNode = $treeBuilder->root('orm');
92
        $rootNode
93 9
            ->addDefaultsIfNotSet()
94 9
            ->children()
95 9
                ->scalarNode('entity_manager')
96 9
                    ->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 9
                    ->defaultValue('default')
98 9
                    ->cannotBeEmpty()
99 9
                ->end()
100 9
            ->end();
101
102 9
        return $rootNode;
103
    }
104
105 9
    protected function addOdm()
106
    {
107 9
        $treeBuilder = new TreeBuilder();
108 9
        $rootNode = $treeBuilder->root('odm');
109
        $rootNode
110 9
            ->addDefaultsIfNotSet()
111 9
            ->children()
112 9
                ->scalarNode('document_manager')
113 9
                    ->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 9
                    ->defaultValue('default')
115 9
                    ->cannotBeEmpty()
116 9
                ->end()
117 9
            ->end();
118
119 9
        return $rootNode;
120
    }
121
122 9
    protected function addManager()
123
    {
124 9
        $treeBuilder = new TreeBuilder();
125 9
        $rootNode = $treeBuilder->root('manager');
126
        $rootNode
127 9
            ->addDefaultsIfNotSet()
128 9
            ->children()
129 9
                ->scalarNode('job')
130 9
                    ->defaultValue('odm')
131 9
                    ->info('This can be [odm|orm|beanstalkd|rabbit_mq|redis|(your-own-custom-one)]')
132 9
                    ->cannotBeEmpty()
133 9
                ->end()
134 9
                ->scalarNode('run')->end()
135 9
                ->scalarNode('job_timing')->end()
136 9
            ->end();
137
138 9
        return $rootNode;
139
    }
140
141 9
    protected function addBeanstalkd()
142
    {
143 9
        $treeBuilder = new TreeBuilder();
144 9
        $rootNode = $treeBuilder->root('beanstalkd');
145
        $rootNode
146 9
            ->children()
147 9
                ->scalarNode('host')->end()
148 9
                ->scalarNode('tube')->end()
149 9
            ->end();
150
151 9
        return $rootNode;
152
    }
153
154 9
    protected function addRetry()
155
    {
156 9
        $treeBuilder = new TreeBuilder();
157 9
        $rootNode = $treeBuilder->root('retry');
158
        $rootNode
159 9
            ->addDefaultsIfNotSet()
160 9
            ->children()
161 9
                ->arrayNode('max')
162 9
                    ->addDefaultsIfNotSet()
163 9
                    ->children()
164 9
                        ->integerNode('retries')
165 9
                            ->info('This the maximum total number of retries of any type.')
166 9
                            ->defaultValue(3)
167 9
                        ->end()
168 9
                        ->integerNode('failures')
169 9
                            ->info('This the maximum total number of failures before a job is marked as hitting the maximum failures.')
170 9
                            ->defaultValue(1)
171 9
                        ->end()
172 9
                        ->integerNode('exceptions')
173 9
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
174 9
                            ->defaultValue(2)
175 9
                        ->end()
176 9
                        ->integerNode('stalls')
177 9
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
178 9
                            ->defaultValue(2)
179 9
                        ->end()
180 9
                    ->end()
181 9
                ->end()
182 9
                ->arrayNode('auto')
183 9
                    ->addDefaultsIfNotSet()
184 9
                    ->children()
185 9
                        ->booleanNode('failure')
186 9
                            ->info('Instantly re-queue the job on failure.')
187 9
                            ->defaultTrue()
188 9
                        ->end()
189 9
                        ->booleanNode('exception')
190 9
                            ->info('Instantly re-queue the job on exception.')
191 9
                            ->defaultFalse()
192 9
                        ->end()
193 9
                    ->end()
194 9
                ->end()
195 9
            ->end();
196
197 9
        return $rootNode;
198
    }
199
200 9
    protected function addPriority()
201
    {
202 9
        $treeBuilder = new TreeBuilder();
203 9
        $rootNode = $treeBuilder->root('priority');
204
        $rootNode
205 9
            ->addDefaultsIfNotSet()
206 9
            ->children()
207 9
                ->integerNode('max')
208 9
                    ->defaultValue(255)
209 9
                    ->info('Maximum priority value.')
210 9
                    ->min(1)
211 9
                ->end()
212 9
                ->enumNode('direction')
213 9
                    ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])
214 9
                    ->info('Whether 1 is high priority or low prioirty.  '.PriorityJobManager::PRIORITY_ASC.' means 1 is low, '.PriorityJobManager::PRIORITY_DESC.' means 1 is high.')
215 9
                    ->defaultValue(PriorityJobManager::PRIORITY_DESC)
216 9
                ->end()
217 9
            ->end();
218
219 9
        return $rootNode;
220
    }
221
222 9
    protected function addClasses()
223
    {
224 9
        $treeBuilder = new TreeBuilder();
225 9
        $rootNode = $treeBuilder->root('class');
226
        $rootNode
227 9
            ->children()
228 9
                ->scalarNode('job')
229 9
                    ->info('If you want to override the Job class, put the class name here.')->end()
230 9
                ->scalarNode('job_archive')
231 9
                    ->info('If you want to override the JobArchive class, put the class name here.')->end()
232 9
                ->scalarNode('job_timing')
233 9
                    ->info('If you want to override the JobTiming class, put the class name here.')->end()
234 9
                ->scalarNode('run')
235 9
                    ->info('If you want to override the Run class, put the class name here.')->end()
236 9
                ->scalarNode('run_archive')
237 9
                    ->info('If you want to override the RunArchive class, put the class name here.')->end()
238 9
            ->end();
239
240 9
        return $rootNode;
241
    }
242
243 9
    protected function addAdmin()
244
    {
245 9
        $treeBuilder = new TreeBuilder();
246 9
        $rootNode = $treeBuilder->root('admin');
247
        $rootNode
248 9
            ->addDefaultsIfNotSet()
249 9
            ->children()
250 9
                ->scalarNode('chartjs')
251 9
                    ->defaultValue('https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js')
252 9
                    ->info('This can be changed to say a locally hosted path or url.')->end()
253 9
                ->end()
254 9
            ->end();
255
256 9
        return $rootNode;
257
    }
258
259 9
    protected function addSncRedis()
260
    {
261 9
        $treeBuilder = new TreeBuilder();
262 9
        $rootNode = $treeBuilder->root('snc_redis');
263
        $rootNode
264 9
            ->children()
265 9
                ->enumNode('type')
266 9
                    ->values(['predis', 'phpredis'])
267 9
                    ->defaultNull()->end()
268 9
                ->scalarNode('alias')
269 9
                    ->defaultNull()->end()
270 9
            ->end()
271 9
            ->validate()->ifTrue(function ($node) {
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 9
            })->thenInvalid('if alias or type is set, then both must be set')->end();
281
282 9
        return $rootNode;
283
    }
284
285 9
    protected function addPredis()
286
    {
287 9
        $treeBuilder = new TreeBuilder();
288 9
        $rootNode = $treeBuilder->root('predis');
289
        $rootNode
290 9
            ->children()
291 9
                ->scalarNode('dsn')->defaultNull()->end()
292 9
                ->append($this->addPredisArgs())
293 9
            ->end()
294 9
            ->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 9
            })->thenInvalid('if dsn is set, do not use connection_parameters for predis (and vice-versa)')->end();
301
302 9
        return $rootNode;
303
    }
304
305 4
    protected function checkPredis(array $node)
306
    {
307 4
        if ((isset($node['predis']['dsn']) || isset($node['predis']['connection_parameters']['host'])) &&
308 4
            (isset($node['snc_redis']['type']) || isset($node['phpredis']['host']))) {
309
            return true;
310
        }
311
312 4
        return false;
313
    }
314
315 4
    protected function checkSncPhpRedis(array $node)
316
    {
317 4
        if (isset($node['snc_redis']['type']) &&
318 4
            isset($node['phpredis']['host'])) {
319
            return true;
320
        }
321
322 4
        return false;
323
    }
324
325 9
    protected function addRedis()
326
    {
327 9
        $treeBuilder = new TreeBuilder();
328 9
        $rootNode = $treeBuilder->root('redis');
329
        $rootNode
330 9
            ->addDefaultsIfNotSet()
331 9
            ->children()
332 9
                ->scalarNode('prefix')->defaultValue('dtc_queue_')->end()
333 9
                ->append($this->addSncRedis())
334 9
                ->append($this->addPredis())
335 9
                ->append($this->addPhpRedisArgs())
336 9
            ->end()
337 9
            ->validate()->ifTrue(function ($node) {
338 4
                if ($this->checkPredis($node)) {
339
                    return true;
340
                }
341 4
                if ($this->checkSncPhpRedis($node)) {
342
                    return true;
343
                }
344
345 4
                return false;
346 9
            })->thenInvalid('only one of [snc_redis | predis | phpredis] should be set')->end();
347
348 9
        return $rootNode;
349
    }
350
351 9
    protected function addPhpRedisArgs()
352
    {
353 9
        $treeBuilder = new TreeBuilder();
354 9
        $rootNode = $treeBuilder->root('phpredis');
355
        $rootNode
356 9
            ->addDefaultsIfNotSet()
357 9
            ->children()
358 9
                ->scalarNode('host')->end()
359 9
                ->integerNode('port')->defaultValue(6379)->end()
360 9
                ->floatNode('timeout')->defaultValue(0)->end()
361 9
                ->integerNode('retry_interval')->defaultNull()->end()
362 9
                ->floatNode('read_timeout')->defaultValue(0)->end()
363 9
                ->scalarNode('auth')->end()
364 9
            ->end()
365 9
            ->validate()->ifTrue(function ($node) {
366 2
                if (!empty($node) && !isset($node['host'])) {
367
                    return true;
368
                }
369
370 2
                return false;
371 9
            })->thenInvalid('phpredis host should be set')->end();
372
373 9
        return $rootNode;
374
    }
375
376 9
    protected function addPredisArgs()
377
    {
378 9
        $treeBuilder = new TreeBuilder();
379 9
        $rootNode = $treeBuilder->root('connection_parameters');
380
        $rootNode
381 9
            ->addDefaultsIfNotSet()
382 9
            ->children()
383 9
                ->scalarNode('scheme')->defaultValue('tcp')->end()
384 9
                ->scalarNode('host')->defaultNull()->end()
385 9
                ->integerNode('port')->defaultNull()->end()
386 9
                ->scalarNode('path')->defaultNull()->end()
387 9
                ->scalarNode('database')->defaultNull()->end()
388 9
                ->scalarNode('password')->defaultNull()->end()
389 9
                ->booleanNode('async')->defaultFalse()->end()
390 9
                ->booleanNode('persistent')->defaultFalse()->end()
391 9
                ->floatNode('timeout')->defaultValue(5.0)->end()
392 9
                ->floatNode('read_write_timeout')->defaultNull()->end()
393 9
                ->scalarNode('alias')->defaultNull()->end()
394 9
                ->integerNode('weight')->defaultNull()->end()
395 9
                ->booleanNode('iterable_multibulk')->defaultFalse()->end()
396 9
                ->booleanNode('throw_errors')->defaultTrue()->end()
397 9
            ->end()
398 9
            ->validate()->ifTrue(function ($node) {
399 1
                if (isset($node['host']) && !isset($node['port'])) {
400
                    return true;
401
                }
402 1
                if (isset($node['port']) && !isset($node['host'])) {
403
                    return true;
404
                }
405
406 1
                return false;
407 9
            })->thenInvalid('predis connection_parameters host and port should both be set')->end();
408
409 9
        return $rootNode;
410
    }
411
412 9
    protected function addRabbitMqOptions()
413
    {
414 9
        $treeBuilder = new TreeBuilder();
415 9
        $rootNode = $treeBuilder->root('options');
416
        $rootNode
417 9
            ->children()
418 9
                ->scalarNode('insist')->end()
419 9
                ->scalarNode('login_method')->end()
420 9
                ->scalarNode('login_response')->end()
421 9
                ->scalarNode('locale')->end()
422 9
                ->floatNode('connection_timeout')->end()
423 9
                ->floatNode('read_write_timeout')->end()
424 9
                ->booleanNode('keepalive')->end()
425 9
                ->integerNode('heartbeat')->end()
426 9
            ->end();
427
428 9
        return $rootNode;
429
    }
430
431 9
    protected function addRabbitMqSslOptions()
432
    {
433 9
        $treeBuilder = new TreeBuilder();
434 9
        $rootNode = $treeBuilder->root('ssl_options');
435
        $rootNode
436 9
            ->prototype('variable')->end()
0 ignored issues
show
Bug introduced by
The method prototype() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

436
            ->/** @scrutinizer ignore-call */ 
437
              prototype('variable')->end()
Loading history...
437 9
            ->validate()
438 9
                ->ifTrue(function ($node) {
439 1
                    if (!is_array($node)) {
440
                        return true;
441
                    }
442 1
                    foreach ($node as $key => $value) {
443 1
                        if (is_array($value)) {
444 1
                            if ('peer_fingerprint' !== $key) {
445 1
                                return true;
446
                            } else {
447 1
                                foreach ($value as $key1 => $value1) {
448 1
                                    if (!is_string($key1) || !is_string($value1)) {
449 1
                                        return true;
450
                                    }
451
                                }
452
                            }
453
                        }
454
                    }
455
456 1
                    return false;
457 9
                })
458 9
                ->thenInvalid('Must be key-value pairs')
459 9
            ->end();
460
461 9
        return $rootNode;
462
    }
463
464 9
    protected function addRabbitMqExchange()
465
    {
466 9
        $treeBuilder = new TreeBuilder();
467 9
        $rootNode = $treeBuilder->root('exchange_args');
468
        $rootNode
469 9
            ->addDefaultsIfNotSet()
470 9
            ->children()
471 9
                ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
472 9
                ->booleanNode('type')->defaultValue('direct')->end()
473 9
                ->booleanNode('passive')->defaultFalse()->end()
474 9
                ->booleanNode('durable')->defaultTrue()->end()
475 9
                ->booleanNode('auto_delete')->defaultFalse()->end()
476 9
            ->end();
477
478 9
        return $rootNode;
479
    }
480
481 9
    protected function addRabbitMqArgs()
482
    {
483 9
        $treeBuilder = new TreeBuilder();
484 9
        $rootNode = $treeBuilder->root('queue_args');
485
        $rootNode
486 9
            ->addDefaultsIfNotSet()
487 9
            ->children()
488 9
                ->scalarNode('queue')->defaultValue('dtc_queue')->end()
489 9
                ->booleanNode('passive')->defaultFalse()->end()
490 9
                ->booleanNode('durable')->defaultTrue()->end()
491 9
                ->booleanNode('exclusive')->defaultFalse()->end()
492 9
                ->booleanNode('auto_delete')->defaultFalse()->end()
493 9
            ->end();
494
495 9
        return $rootNode;
496
    }
497
498 9
    protected function addRabbitMq()
499
    {
500 9
        $treeBuilder = new TreeBuilder();
501 9
        $rootNode = $treeBuilder->root('rabbit_mq');
502
        $rootNode
503 9
            ->children()
504 9
                ->scalarNode('host')->end()
505 9
                ->scalarNode('port')->end()
506 9
                ->scalarNode('user')->end()
507 9
                ->scalarNode('password')->end()
508 9
                ->scalarNode('vhost')->defaultValue('/')->end()
509 9
                ->booleanNode('ssl')->defaultFalse()->end()
510 9
                ->append($this->addRabbitMqOptions())
511 9
                ->append($this->addRabbitMqSslOptions())
512 9
                ->append($this->addRabbitMqArgs())
513 9
                ->append($this->addRabbitMqExchange())
514 9
            ->end()
515 9
            ->validate()->always(function ($node) {
516 1
                if (empty($node['ssl_options'])) {
517 1
                    unset($node['ssl_options']);
518
                }
519 1
                if (empty($node['options'])) {
520 1
                    unset($node['options']);
521
                }
522
523 1
                return $node;
524 9
            })->end()
525 9
            ->validate()->ifTrue(function ($node) {
526 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
527 1
                    return true;
528
                }
529
530 1
                return false;
531 9
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
532
        ->end();
533
534
        return $rootNode;
535
    }
536
}
537