Passed
Push — master ( 26ea1a...43abdf )
by Matthew
07:12
created

Configuration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 194
c 1
b 0
f 0
dl 0
loc 276
ccs 176
cts 184
cp 0.9565
rs 10
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setDeprecatedNode() 0 9 2
A addManager() 0 23 2
A getConfigTreeBuilder() 0 45 2
A addSimpleScalar() 0 22 2
A addTimings() 0 27 2
A addPriority() 0 26 2
A addClasses() 0 26 2
A addRetry() 0 51 2
A addBeanstalkd() 0 21 2
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
    use RedisConfiguration;
13
    use RabbitMQConfiguration;
14
15
    /**
16
     * Generates the configuration tree.
17
     *
18
     * @return TreeBuilder
19
     */
20 10
    public function getConfigTreeBuilder()
21
    {
22 10
        $treeBuilder = new TreeBuilder('dtc_queue');
23
24 10
        if (method_exists($treeBuilder, 'getRootNode')) {
25 10
            $rootNode = $treeBuilder->getRootNode();
26
        } else {
27
            // BC layer for symfony/config 4.1 and older
28
            $rootNode = $treeBuilder->root('dtc_queue');
0 ignored issues
show
Bug introduced by
The method root() does not exist on Symfony\Component\Config...ion\Builder\TreeBuilder. ( Ignorable by Annotation )

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

28
            /** @scrutinizer ignore-call */ 
29
            $rootNode = $treeBuilder->root('dtc_queue');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        }
30
31
        $node = $rootNode
32 10
            ->children()
33 10
                ->booleanNode('locale_fix')
34 10
                    ->defaultFalse()
35 10
                    ->info('Set this to true to fix issues with ORM saving (see issue #98) in non-period decimal format locales')
36 10
                ->end()
37 10
                ->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'))
38 10
                ->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'))
39 10
                ->append($this->addManager())
40 10
                ->append($this->addTimings())
41 10
                ->append($this->addBeanstalkd())
42 10
                ->append($this->addRabbitMq())
43 10
                ->append($this->addRedis())
44 10
                ->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'))
45 10
                ->append($this->addClasses())
46 10
                ->append($this->addPriority())
47 10
                ->append($this->addRetry());
48 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'document_manager', 'The "%node% option is deprecated, Use "odm: { document_manager: ... }" instead.');
49 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'entity_manager', 'The "%node% option is deprecated, Use "orm: { entity_manager: ... }" instead.');
50 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'default_manager', 'The "%node% option is deprecated, Use "manager: { job: ... }" instead.');
51 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'run_manager', 'The "%node% option is deprecated, Use "manager: { run: ... }" instead.');
52 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'job_timing_manager', 'The "%node% option is deprecated, Use "manager: { job_timing: ... }" instead.');
53 10
        $node = $this->setDeprecatedNode($node, 'booleanNode', 'record_timings', 'The "%node% option is deprecated, Use "timings: { record: ... }" instead.');
54 10
        $node = $this->setDeprecatedNode($node, 'floatNode', 'record_timings_timezone_offset', 'The "%node% option is deprecated, Use "record: { timezone_offset: ... }" instead.');
55 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job', 'The "%node% option is deprecated, Use "class: { job: ... }" instead.');
56 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_archive', 'The "%node% option is deprecated, Use "class: { job_archive: ... }" instead.');
57 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run', 'The "%node% option is deprecated, Use "class: { run: ... }" instead.');
58 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run_archive', 'The "%node% option is deprecated, Use "class: { run_archive: ... }" instead.');
59 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_timing', 'The "%node% option is deprecated, Use "class: { job_timing: ... }" instead.');
60 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_max', 'The "%node% option is deprecated, Use "priority: { max: ... }" instead.');
61 10
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_direction', 'The "%node% option is deprecated, Use "priority: { direction: ... }" instead.');
62 10
        $node->end();
63
64 10
        return $treeBuilder;
65
    }
66
67 10
    public function setDeprecatedNode($node, $type, $name, $deprecatedMessage)
68
    {
69 10
        $node = $node->$type($name);
70
71 10
        if (Kernel::VERSION_ID >= 30400) {
72 10
            $node = $node->setDeprecated($deprecatedMessage);
73
        }
74
75 10
        return $node->end();
76
    }
77
78 10
    protected function addTimings()
79
    {
80 10
        $treeBuilder = new TreeBuilder('timings');
81
82 10
        if (method_exists($treeBuilder, 'getRootNode')) {
83 10
            $rootNode = $treeBuilder->getRootNode();
84
        } else {
85
            // BC layer for symfony/config 4.1 and older
86
            $rootNode = $treeBuilder->root('timings');
87
        }
88
89
        $rootNode
90 10
            ->addDefaultsIfNotSet()
91 10
            ->children()
92 10
                ->booleanNode('record')
93 10
                    ->info('Set this to true to record timings (used on the Trends page)')
94 10
                    ->defaultFalse()
95 10
                ->end()
96 10
                ->floatNode('timezone_offset')
97 10
                    ->defaultValue(0)
98 10
                    ->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')
99 10
                    ->max(24)
100 10
                    ->min(-24)
101 10
                ->end()
102 10
            ->end();
103
104 10
        return $rootNode;
105
    }
106
107 10
    protected function addSimpleScalar($rootName, $nodeName, $info, $defaultValue = 'default')
108
    {
109 10
        $treeBuilder = new TreeBuilder($rootName);
110
111 10
        if (method_exists($treeBuilder, 'getRootNode')) {
112 10
            $rootNode = $treeBuilder->getRootNode();
113
        } else {
114
            // BC layer for symfony/config 4.1 and older
115
            $rootNode = $treeBuilder->root($rootName);
116
        }
117
118
        $rootNode
119 10
            ->addDefaultsIfNotSet()
120 10
            ->children()
121 10
            ->scalarNode($nodeName)
122 10
            ->info($info)
123 10
            ->defaultValue($defaultValue)
124 10
            ->cannotBeEmpty()
125 10
            ->end()
126 10
            ->end();
127
128 10
        return $rootNode;
129
    }
130
131 10
    protected function addManager()
132
    {
133 10
        $treeBuilder = new TreeBuilder('manager');
134
135 10
        if (method_exists($treeBuilder, 'getRootNode')) {
136 10
            $rootNode = $treeBuilder->getRootNode();
137
        } else {
138
            // BC layer for symfony/config 4.1 and older
139
            $rootNode = $treeBuilder->root('manager');
140
        }
141
        $rootNode
142 10
            ->addDefaultsIfNotSet()
143 10
            ->children()
144 10
                ->scalarNode('job')
145 10
                    ->defaultValue('odm')
146 10
                    ->info('This can be [odm|orm|beanstalkd|rabbit_mq|redis|(your-own-custom-one)]')
147 10
                    ->cannotBeEmpty()
148 10
                ->end()
149 10
                ->scalarNode('run')->end()
150 10
                ->scalarNode('job_timing')->end()
151 10
            ->end();
152
153 10
        return $rootNode;
154
    }
155
156 10
    protected function addBeanstalkd()
157
    {
158 10
        $treeBuilder = new TreeBuilder('beanstalkd');
159
160 10
        if (method_exists($treeBuilder, 'getRootNode')) {
161 10
            $rootNode = $treeBuilder->getRootNode();
162
        } else {
163
            // BC layer for symfony/config 4.1 and older
164
            $rootNode = $treeBuilder->root('beanstalkd');
165
        }
166
167
        $rootNode
168 10
            ->children()
169 10
                ->scalarNode('host')->end()
170 10
                ->integerNode('port')
171 10
                    ->defaultValue(11300)
172 10
                ->end()
173 10
                ->scalarNode('tube')->end()
174 10
            ->end();
175
176 10
        return $rootNode;
177
    }
178
179 10
    protected function addRetry()
180
    {
181 10
        $treeBuilder = new TreeBuilder('retry');
182
183 10
        if (method_exists($treeBuilder, 'getRootNode')) {
184 10
            $rootNode = $treeBuilder->getRootNode();
185
        } else {
186
            // BC layer for symfony/config 4.1 and older
187
            $rootNode = $treeBuilder->root('retry');
188
        }
189
190
        $rootNode
191 10
            ->addDefaultsIfNotSet()
192 10
            ->children()
193 10
                ->arrayNode('max')
194 10
                    ->addDefaultsIfNotSet()
195 10
                    ->children()
196 10
                        ->integerNode('retries')
197 10
                            ->info('This the maximum total number of retries of any type.')
198 10
                            ->defaultValue(3)
199 10
                        ->end()
200 10
                        ->integerNode('failures')
201 10
                            ->info('This the maximum total number of failures before a job is marked as hitting the maximum failures.')
202 10
                            ->defaultValue(1)
203 10
                        ->end()
204 10
                        ->integerNode('exceptions')
205 10
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
206 10
                            ->defaultValue(2)
207 10
                        ->end()
208 10
                        ->integerNode('stalls')
209 10
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
210 10
                            ->defaultValue(2)
211 10
                        ->end()
212 10
                    ->end()
213 10
                ->end()
214 10
                ->arrayNode('auto')
215 10
                    ->addDefaultsIfNotSet()
216 10
                    ->children()
217 10
                        ->booleanNode('failure')
218 10
                            ->info('Instantly re-queue the job on failure.')
219 10
                            ->defaultTrue()
220 10
                        ->end()
221 10
                        ->booleanNode('exception')
222 10
                            ->info('Instantly re-queue the job on exception.')
223 10
                            ->defaultFalse()
224 10
                        ->end()
225 10
                    ->end()
226 10
                ->end()
227 10
            ->end();
228
229 10
        return $rootNode;
230
    }
231
232 10
    protected function addPriority()
233
    {
234 10
        $treeBuilder = new TreeBuilder('priority');
235
236 10
        if (method_exists($treeBuilder, 'getRootNode')) {
237 10
            $rootNode = $treeBuilder->getRootNode();
238
        } else {
239
            // BC layer for symfony/config 4.1 and older
240
            $rootNode = $treeBuilder->root('priority');
241
        }
242
        $rootNode
243 10
            ->addDefaultsIfNotSet()
244 10
            ->children()
245 10
                ->integerNode('max')
246 10
                    ->defaultValue(255)
247 10
                    ->info('Maximum priority value.')
248 10
                    ->min(1)
249 10
                ->end()
250 10
                ->enumNode('direction')
251 10
                    ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])
252 10
                    ->info('Whether 1 is high priority or low prioirty.  '.PriorityJobManager::PRIORITY_ASC.' means 1 is low, '.PriorityJobManager::PRIORITY_DESC.' means 1 is high.')
253 10
                    ->defaultValue(PriorityJobManager::PRIORITY_DESC)
254 10
                ->end()
255 10
            ->end();
256
257 10
        return $rootNode;
258
    }
259
260 10
    protected function addClasses()
261
    {
262 10
        $treeBuilder = new TreeBuilder('class');
263
264 10
        if (method_exists($treeBuilder, 'getRootNode')) {
265 10
            $rootNode = $treeBuilder->getRootNode();
266
        } else {
267
            // BC layer for symfony/config 4.1 and older
268
            $rootNode = $treeBuilder->root('class');
269
        }
270
271
        $rootNode
272 10
            ->children()
273 10
                ->scalarNode('job')
274 10
                    ->info('If you want to override the Job class, put the class name here.')->end()
275 10
                ->scalarNode('job_archive')
276 10
                    ->info('If you want to override the JobArchive class, put the class name here.')->end()
277 10
                ->scalarNode('job_timing')
278 10
                    ->info('If you want to override the JobTiming class, put the class name here.')->end()
279 10
                ->scalarNode('run')
280 10
                    ->info('If you want to override the Run class, put the class name here.')->end()
281 10
                ->scalarNode('run_archive')
282 10
                    ->info('If you want to override the RunArchive class, put the class name here.')->end()
283 10
            ->end();
284
285 10
        return $rootNode;
286
    }
287
}
288