Passed
Push — master ( f67228...ea1bad )
by Matthew
08:44
created

Configuration::addPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
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->addSimpleScalar('orm', 'entity_manager', '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'))
25 9
                ->append($this->addSimpleScalar('odm', 'document_manager', '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'))
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->addSimpleScalar('admin', 'chartjs', 'This can be changed to say a locally hosted path or url.', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js'))
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 addSimpleScalar($rootName, $nodeName, $info, $defaultValue = 'default')
89
    {
90 9
        $treeBuilder = new TreeBuilder();
91 9
        $rootNode = $treeBuilder->root($rootName);
92
        $rootNode
93 9
            ->addDefaultsIfNotSet()
94 9
            ->children()
95 9
            ->scalarNode($nodeName)
96 9
            ->info($info)
97 9
            ->defaultValue($defaultValue)
98 9
            ->cannotBeEmpty()
99 9
            ->end()
100 9
            ->end();
101
102 9
        return $rootNode;
103
    }
104
105 9
    protected function addManager()
106
    {
107 9
        $treeBuilder = new TreeBuilder();
108 9
        $rootNode = $treeBuilder->root('manager');
109
        $rootNode
110 9
            ->addDefaultsIfNotSet()
111 9
            ->children()
112 9
                ->scalarNode('job')
113 9
                    ->defaultValue('odm')
114 9
                    ->info('This can be [odm|orm|beanstalkd|rabbit_mq|redis|(your-own-custom-one)]')
115 9
                    ->cannotBeEmpty()
116 9
                ->end()
117 9
                ->scalarNode('run')->end()
118 9
                ->scalarNode('job_timing')->end()
119 9
            ->end();
120
121 9
        return $rootNode;
122
    }
123
124 9
    protected function addBeanstalkd()
125
    {
126 9
        $treeBuilder = new TreeBuilder();
127 9
        $rootNode = $treeBuilder->root('beanstalkd');
128
        $rootNode
129 9
            ->children()
130 9
                ->scalarNode('host')->end()
131 9
                ->scalarNode('tube')->end()
132 9
            ->end();
133
134 9
        return $rootNode;
135
    }
136
137 9
    protected function addRetry()
138
    {
139 9
        $treeBuilder = new TreeBuilder();
140 9
        $rootNode = $treeBuilder->root('retry');
141
        $rootNode
142 9
            ->addDefaultsIfNotSet()
143 9
            ->children()
144 9
                ->arrayNode('max')
145 9
                    ->addDefaultsIfNotSet()
146 9
                    ->children()
147 9
                        ->integerNode('retries')
148 9
                            ->info('This the maximum total number of retries of any type.')
149 9
                            ->defaultValue(3)
150 9
                        ->end()
151 9
                        ->integerNode('failures')
152 9
                            ->info('This the maximum total number of failures before a job is marked as hitting the maximum failures.')
153 9
                            ->defaultValue(1)
154 9
                        ->end()
155 9
                        ->integerNode('exceptions')
156 9
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
157 9
                            ->defaultValue(2)
158 9
                        ->end()
159 9
                        ->integerNode('stalls')
160 9
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
161 9
                            ->defaultValue(2)
162 9
                        ->end()
163 9
                    ->end()
164 9
                ->end()
165 9
                ->arrayNode('auto')
166 9
                    ->addDefaultsIfNotSet()
167 9
                    ->children()
168 9
                        ->booleanNode('failure')
169 9
                            ->info('Instantly re-queue the job on failure.')
170 9
                            ->defaultTrue()
171 9
                        ->end()
172 9
                        ->booleanNode('exception')
173 9
                            ->info('Instantly re-queue the job on exception.')
174 9
                            ->defaultFalse()
175 9
                        ->end()
176 9
                    ->end()
177 9
                ->end()
178 9
            ->end();
179
180 9
        return $rootNode;
181
    }
182
183 9
    protected function addPriority()
184
    {
185 9
        $treeBuilder = new TreeBuilder();
186 9
        $rootNode = $treeBuilder->root('priority');
187
        $rootNode
188 9
            ->addDefaultsIfNotSet()
189 9
            ->children()
190 9
                ->integerNode('max')
191 9
                    ->defaultValue(255)
192 9
                    ->info('Maximum priority value.')
193 9
                    ->min(1)
194 9
                ->end()
195 9
                ->enumNode('direction')
196 9
                    ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])
197 9
                    ->info('Whether 1 is high priority or low prioirty.  '.PriorityJobManager::PRIORITY_ASC.' means 1 is low, '.PriorityJobManager::PRIORITY_DESC.' means 1 is high.')
198 9
                    ->defaultValue(PriorityJobManager::PRIORITY_DESC)
199 9
                ->end()
200 9
            ->end();
201
202 9
        return $rootNode;
203
    }
204
205 9
    protected function addClasses()
206
    {
207 9
        $treeBuilder = new TreeBuilder();
208 9
        $rootNode = $treeBuilder->root('class');
209
        $rootNode
210 9
            ->children()
211 9
                ->scalarNode('job')
212 9
                    ->info('If you want to override the Job class, put the class name here.')->end()
213 9
                ->scalarNode('job_archive')
214 9
                    ->info('If you want to override the JobArchive class, put the class name here.')->end()
215 9
                ->scalarNode('job_timing')
216 9
                    ->info('If you want to override the JobTiming class, put the class name here.')->end()
217 9
                ->scalarNode('run')
218 9
                    ->info('If you want to override the Run class, put the class name here.')->end()
219 9
                ->scalarNode('run_archive')
220 9
                    ->info('If you want to override the RunArchive class, put the class name here.')->end()
221 9
            ->end();
222
223 9
        return $rootNode;
224
    }
225
226 9
    protected function addSncRedis()
227
    {
228 9
        $treeBuilder = new TreeBuilder();
229 9
        $rootNode = $treeBuilder->root('snc_redis');
230
        $rootNode
231 9
            ->children()
232 9
                ->enumNode('type')
233 9
                    ->values(['predis', 'phpredis'])
234 9
                    ->defaultNull()->end()
235 9
                ->scalarNode('alias')
236 9
                    ->defaultNull()->end()
237 9
            ->end()
238 9
            ->validate()->ifTrue(function ($node) {
239 1
                if (isset($node['type']) && !isset($node['alias'])) {
240 1
                    return true;
241
                }
242 1
                if (isset($node['alias']) && !isset($node['type'])) {
243 1
                    return true;
244
                }
245
246 1
                return false;
247 9
            })->thenInvalid('if alias or type is set, then both must be set')->end();
248
249 9
        return $rootNode;
250
    }
251
252 9
    protected function addPredis()
253
    {
254 9
        $treeBuilder = new TreeBuilder();
255 9
        $rootNode = $treeBuilder->root('predis');
256
        $rootNode
257 9
            ->children()
258 9
                ->scalarNode('dsn')->defaultNull()->end()
259 9
                ->append($this->addPredisArgs())
260 9
            ->end()
261 9
            ->validate()->ifTrue(function ($node) {
262 1
                if (isset($node['dsn']) && (isset($node['connection_parameters']['host']) || isset($node['connection_parameters']['port']))) {
263
                    return true;
264
                }
265
266 1
                return false;
267 9
            })->thenInvalid('if dsn is set, do not use connection_parameters for predis (and vice-versa)')->end();
268
269 9
        return $rootNode;
270
    }
271
272 4
    protected function checkPredis(array $node)
273
    {
274 4
        if ((isset($node['predis']['dsn']) || isset($node['predis']['connection_parameters']['host'])) &&
275 4
            (isset($node['snc_redis']['type']) || isset($node['phpredis']['host']))) {
276
            return true;
277
        }
278
279 4
        return false;
280
    }
281
282 4
    protected function checkSncPhpRedis(array $node)
283
    {
284 4
        if (isset($node['snc_redis']['type']) &&
285 4
            isset($node['phpredis']['host'])) {
286
            return true;
287
        }
288
289 4
        return false;
290
    }
291
292 9
    protected function addRedis()
293
    {
294 9
        $treeBuilder = new TreeBuilder();
295 9
        $rootNode = $treeBuilder->root('redis');
296
        $rootNode
297 9
            ->addDefaultsIfNotSet()
298 9
            ->children()
299 9
                ->scalarNode('prefix')->defaultValue('dtc_queue_')->end()
300 9
                ->append($this->addSncRedis())
301 9
                ->append($this->addPredis())
302 9
                ->append($this->addPhpRedisArgs())
303 9
            ->end()
304 9
            ->validate()->ifTrue(function ($node) {
305 4
                if ($this->checkPredis($node)) {
306
                    return true;
307
                }
308 4
                if ($this->checkSncPhpRedis($node)) {
309
                    return true;
310
                }
311
312 4
                return false;
313 9
            })->thenInvalid('only one of [snc_redis | predis | phpredis] should be set')->end();
314
315 9
        return $rootNode;
316
    }
317
318 9
    protected function addPhpRedisArgs()
319
    {
320 9
        $treeBuilder = new TreeBuilder();
321 9
        $rootNode = $treeBuilder->root('phpredis');
322
        $rootNode
323 9
            ->addDefaultsIfNotSet()
324 9
            ->children()
325 9
                ->scalarNode('host')->end()
326 9
                ->integerNode('port')->defaultValue(6379)->end()
327 9
                ->floatNode('timeout')->defaultValue(0)->end()
328 9
                ->integerNode('retry_interval')->defaultNull()->end()
329 9
                ->floatNode('read_timeout')->defaultValue(0)->end()
330 9
                ->scalarNode('auth')->end()
331 9
            ->end()
332 9
            ->validate()->ifTrue(function ($node) {
333 2
                if (!empty($node) && !isset($node['host'])) {
334
                    return true;
335
                }
336
337 2
                return false;
338 9
            })->thenInvalid('phpredis host should be set')->end();
339
340 9
        return $rootNode;
341
    }
342
343 9
    protected function addPredisArgs()
344
    {
345 9
        $treeBuilder = new TreeBuilder();
346 9
        $rootNode = $treeBuilder->root('connection_parameters');
347
        $rootNode
348 9
            ->addDefaultsIfNotSet()
349 9
            ->children()
350 9
                ->scalarNode('scheme')->defaultValue('tcp')->end()
351 9
                ->scalarNode('host')->defaultNull()->end()
352 9
                ->integerNode('port')->defaultNull()->end()
353 9
                ->scalarNode('path')->defaultNull()->end()
354 9
                ->scalarNode('database')->defaultNull()->end()
355 9
                ->scalarNode('password')->defaultNull()->end()
356 9
                ->booleanNode('async')->defaultFalse()->end()
357 9
                ->booleanNode('persistent')->defaultFalse()->end()
358 9
                ->floatNode('timeout')->defaultValue(5.0)->end()
359 9
                ->floatNode('read_write_timeout')->defaultNull()->end()
360 9
                ->scalarNode('alias')->defaultNull()->end()
361 9
                ->integerNode('weight')->defaultNull()->end()
362 9
                ->booleanNode('iterable_multibulk')->defaultFalse()->end()
363 9
                ->booleanNode('throw_errors')->defaultTrue()->end()
364 9
            ->end()
365 9
            ->validate()->ifTrue(function ($node) {
366 1
                if (isset($node['host']) && !isset($node['port'])) {
367
                    return true;
368
                }
369 1
                if (isset($node['port']) && !isset($node['host'])) {
370
                    return true;
371
                }
372
373 1
                return false;
374 9
            })->thenInvalid('predis connection_parameters host and port should both be set')->end();
375
376 9
        return $rootNode;
377
    }
378
379 9
    protected function addRabbitMqOptions()
380
    {
381 9
        $treeBuilder = new TreeBuilder();
382 9
        $rootNode = $treeBuilder->root('options');
383
        $rootNode
384 9
            ->children()
385 9
                ->scalarNode('insist')->end()
386 9
                ->scalarNode('login_method')->end()
387 9
                ->scalarNode('login_response')->end()
388 9
                ->scalarNode('locale')->end()
389 9
                ->floatNode('connection_timeout')->end()
390 9
                ->floatNode('read_write_timeout')->end()
391 9
                ->booleanNode('keepalive')->end()
392 9
                ->integerNode('heartbeat')->end()
393 9
            ->end();
394
395 9
        return $rootNode;
396
    }
397
398 9
    protected function addRabbitMqSslOptions()
399
    {
400 9
        $treeBuilder = new TreeBuilder();
401 9
        $rootNode = $treeBuilder->root('ssl_options');
402
        $rootNode
403 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

403
            ->/** @scrutinizer ignore-call */ 
404
              prototype('variable')->end()
Loading history...
404 9
            ->validate()
405 9
                ->ifTrue(function ($node) {
406 1
                    if (!is_array($node)) {
407
                        return true;
408
                    }
409 1
                    foreach ($node as $key => $value) {
410 1
                        if (is_array($value)) {
411 1
                            if ('peer_fingerprint' !== $key) {
412 1
                                return true;
413
                            } else {
414 1
                                foreach ($value as $key1 => $value1) {
415 1
                                    if (!is_string($key1) || !is_string($value1)) {
416 1
                                        return true;
417
                                    }
418
                                }
419
                            }
420
                        }
421
                    }
422
423 1
                    return false;
424 9
                })
425 9
                ->thenInvalid('Must be key-value pairs')
426 9
            ->end();
427
428 9
        return $rootNode;
429
    }
430
431 9
    protected function addRabbitMqExchange()
432
    {
433 9
        $treeBuilder = new TreeBuilder();
434 9
        $rootNode = $treeBuilder->root('exchange_args');
435
        $rootNode
436 9
            ->addDefaultsIfNotSet()
437 9
            ->children()
438 9
                ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
439 9
                ->booleanNode('type')->defaultValue('direct')->end()
440 9
                ->booleanNode('passive')->defaultFalse()->end()
441 9
                ->booleanNode('durable')->defaultTrue()->end()
442 9
                ->booleanNode('auto_delete')->defaultFalse()->end()
443 9
            ->end();
444
445 9
        return $rootNode;
446
    }
447
448 9
    protected function addRabbitMqArgs()
449
    {
450 9
        $treeBuilder = new TreeBuilder();
451 9
        $rootNode = $treeBuilder->root('queue_args');
452
        $rootNode
453 9
            ->addDefaultsIfNotSet()
454 9
            ->children()
455 9
                ->scalarNode('queue')->defaultValue('dtc_queue')->end()
456 9
                ->booleanNode('passive')->defaultFalse()->end()
457 9
                ->booleanNode('durable')->defaultTrue()->end()
458 9
                ->booleanNode('exclusive')->defaultFalse()->end()
459 9
                ->booleanNode('auto_delete')->defaultFalse()->end()
460 9
            ->end();
461
462 9
        return $rootNode;
463
    }
464
465 9
    protected function addRabbitMq()
466
    {
467 9
        $treeBuilder = new TreeBuilder();
468 9
        $rootNode = $treeBuilder->root('rabbit_mq');
469
        $rootNode
470 9
            ->children()
471 9
                ->scalarNode('host')->end()
472 9
                ->scalarNode('port')->end()
473 9
                ->scalarNode('user')->end()
474 9
                ->scalarNode('password')->end()
475 9
                ->scalarNode('vhost')->defaultValue('/')->end()
476 9
                ->booleanNode('ssl')->defaultFalse()->end()
477 9
                ->append($this->addRabbitMqOptions())
478 9
                ->append($this->addRabbitMqSslOptions())
479 9
                ->append($this->addRabbitMqArgs())
480 9
                ->append($this->addRabbitMqExchange())
481 9
            ->end()
482 9
            ->validate()->always(function ($node) {
483 1
                if (empty($node['ssl_options'])) {
484 1
                    unset($node['ssl_options']);
485
                }
486 1
                if (empty($node['options'])) {
487 1
                    unset($node['options']);
488
                }
489
490 1
                return $node;
491 9
            })->end()
492 9
            ->validate()->ifTrue(function ($node) {
493 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
494 1
                    return true;
495
                }
496
497 1
                return false;
498 9
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
499
        ->end();
500
501
        return $rootNode;
502
    }
503
}
504