Passed
Pull Request — master (#111)
by Artem
07:44
created

Configuration   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Test Coverage

Coverage 95.48%

Importance

Changes 0
Metric Value
eloc 187
dl 0
loc 270
ccs 169
cts 177
cp 0.9548
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setDeprecatedNode() 0 9 2
A addPriority() 0 26 2
A addClasses() 0 26 2
A addManager() 0 23 2
A addRetry() 0 51 2
A addBeanstalkd() 0 18 2
A getConfigTreeBuilder() 0 42 2
A addSimpleScalar() 0 22 2
A addTimings() 0 27 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 9
    public function getConfigTreeBuilder()
21
    {
22 9
        $treeBuilder = new TreeBuilder('dtc_queue');
23
24 9
        if (method_exists($treeBuilder, 'getRootNode')) {
25 9
            $rootNode = $treeBuilder->getRootNode();
26
        } else {
27
            // BC layer for symfony/config 4.1 and older
28
            $rootNode = $treeBuilder->root('dtc_queue');
29
        }
30
31
        $node = $rootNode
32 9
            ->children()
33 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'))
34 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'))
35 9
                ->append($this->addManager())
36 9
                ->append($this->addTimings())
37 9
                ->append($this->addBeanstalkd())
38 9
                ->append($this->addRabbitMq())
39 9
                ->append($this->addRedis())
40 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'))
41 9
                ->append($this->addClasses())
42 9
                ->append($this->addPriority())
43 9
                ->append($this->addRetry());
44
45 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'document_manager', 'The "%node% option is deprecated, Use "odm: { document_manager: ... }" instead.');
46 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'entity_manager', 'The "%node% option is deprecated, Use "orm: { entity_manager: ... }" instead.');
47 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'default_manager', 'The "%node% option is deprecated, Use "manager: { job: ... }" instead.');
48 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'run_manager', 'The "%node% option is deprecated, Use "manager: { run: ... }" instead.');
49 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'job_timing_manager', 'The "%node% option is deprecated, Use "manager: { job_timing: ... }" instead.');
50 9
        $node = $this->setDeprecatedNode($node, 'booleanNode', 'record_timings', 'The "%node% option is deprecated, Use "timings: { record: ... }" instead.');
51 9
        $node = $this->setDeprecatedNode($node, 'floatNode', 'record_timings_timezone_offset', 'The "%node% option is deprecated, Use "record: { timezone_offset: ... }" instead.');
52 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job', 'The "%node% option is deprecated, Use "class: { job: ... }" instead.');
53 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_archive', 'The "%node% option is deprecated, Use "class: { job_archive: ... }" instead.');
54 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run', 'The "%node% option is deprecated, Use "class: { run: ... }" instead.');
55 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run_archive', 'The "%node% option is deprecated, Use "class: { run_archive: ... }" instead.');
56 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_timing', 'The "%node% option is deprecated, Use "class: { job_timing: ... }" instead.');
57 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_max', 'The "%node% option is deprecated, Use "priority: { max: ... }" instead.');
58 9
        $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_direction', 'The "%node% option is deprecated, Use "priority: { direction: ... }" instead.');
59 9
        $node->end();
60
61 9
        return $treeBuilder;
62
    }
63
64 9
    public function setDeprecatedNode($node, $type, $name, $deprecatedMessage)
65
    {
66 9
        $node = $node->$type($name);
67
68 9
        if (Kernel::VERSION_ID >= 30400) {
69 9
            $node = $node->setDeprecated($deprecatedMessage);
70
        }
71
72 9
        return $node->end();
73
    }
74
75 9
    protected function addTimings()
76
    {
77 9
        $treeBuilder = new TreeBuilder('timings');
78
79 9
        if (method_exists($treeBuilder, 'getRootNode')) {
80 9
            $rootNode = $treeBuilder->getRootNode();
81
        } else {
82
            // BC layer for symfony/config 4.1 and older
83
            $rootNode = $treeBuilder->root('timings');
84
        }
85
86
        $rootNode
87 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

87
            ->/** @scrutinizer ignore-call */ 
88
              addDefaultsIfNotSet()
Loading history...
88 9
            ->children()
89 9
                ->booleanNode('record')
90 9
                    ->info('Set this to true to record timings (used on the Trends page)')
91 9
                    ->defaultFalse()
92 9
                ->end()
93 9
                ->floatNode('timezone_offset')
94 9
                    ->defaultValue(0)
95 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')
96 9
                    ->max(24)
97 9
                    ->min(-24)
98 9
                ->end()
99 9
            ->end();
100
101 9
        return $rootNode;
102
    }
103
104 9
    protected function addSimpleScalar($rootName, $nodeName, $info, $defaultValue = 'default')
105
    {
106 9
        $treeBuilder = new TreeBuilder($rootName);
107
108 9
        if (method_exists($treeBuilder, 'getRootNode')) {
109 9
            $rootNode = $treeBuilder->getRootNode();
110
        } else {
111
            // BC layer for symfony/config 4.1 and older
112
            $rootNode = $treeBuilder->root($rootName);
113
        }
114
115
        $rootNode
116 9
            ->addDefaultsIfNotSet()
117 9
            ->children()
118 9
            ->scalarNode($nodeName)
119 9
            ->info($info)
120 9
            ->defaultValue($defaultValue)
121 9
            ->cannotBeEmpty()
122 9
            ->end()
123 9
            ->end();
124
125 9
        return $rootNode;
126
    }
127
128 9
    protected function addManager()
129
    {
130 9
        $treeBuilder = new TreeBuilder('manager');
131
132 9
        if (method_exists($treeBuilder, 'getRootNode')) {
133 9
            $rootNode = $treeBuilder->getRootNode();
134
        } else {
135
            // BC layer for symfony/config 4.1 and older
136
            $rootNode = $treeBuilder->root('manager');
137
        }
138
        $rootNode
139 9
            ->addDefaultsIfNotSet()
140 9
            ->children()
141 9
                ->scalarNode('job')
142 9
                    ->defaultValue('odm')
143 9
                    ->info('This can be [odm|orm|beanstalkd|rabbit_mq|redis|(your-own-custom-one)]')
144 9
                    ->cannotBeEmpty()
145 9
                ->end()
146 9
                ->scalarNode('run')->end()
147 9
                ->scalarNode('job_timing')->end()
148 9
            ->end();
149
150 9
        return $rootNode;
151
    }
152
153 9
    protected function addBeanstalkd()
154
    {
155 9
        $treeBuilder = new TreeBuilder('beanstalkd');
156
157 9
        if (method_exists($treeBuilder, 'getRootNode')) {
158 9
            $rootNode = $treeBuilder->getRootNode();
159
        } else {
160
            // BC layer for symfony/config 4.1 and older
161
            $rootNode = $treeBuilder->root('beanstalkd');
162
        }
163
164
        $rootNode
165 9
            ->children()
166 9
                ->scalarNode('host')->end()
167 9
                ->scalarNode('tube')->end()
168 9
            ->end();
169
170 9
        return $rootNode;
171
    }
172
173 9
    protected function addRetry()
174
    {
175 9
        $treeBuilder = new TreeBuilder('retry');
176
177 9
        if (method_exists($treeBuilder, 'getRootNode')) {
178 9
            $rootNode = $treeBuilder->getRootNode();
179
        } else {
180
            // BC layer for symfony/config 4.1 and older
181
            $rootNode = $treeBuilder->root('retry');
182
        }
183
184
        $rootNode
185 9
            ->addDefaultsIfNotSet()
186 9
            ->children()
187 9
                ->arrayNode('max')
188 9
                    ->addDefaultsIfNotSet()
189 9
                    ->children()
190 9
                        ->integerNode('retries')
191 9
                            ->info('This the maximum total number of retries of any type.')
192 9
                            ->defaultValue(3)
193 9
                        ->end()
194 9
                        ->integerNode('failures')
195 9
                            ->info('This the maximum total number of failures before a job is marked as hitting the maximum failures.')
196 9
                            ->defaultValue(1)
197 9
                        ->end()
198 9
                        ->integerNode('exceptions')
199 9
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
200 9
                            ->defaultValue(2)
201 9
                        ->end()
202 9
                        ->integerNode('stalls')
203 9
                            ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.')
204 9
                            ->defaultValue(2)
205 9
                        ->end()
206 9
                    ->end()
207 9
                ->end()
208 9
                ->arrayNode('auto')
209 9
                    ->addDefaultsIfNotSet()
210 9
                    ->children()
211 9
                        ->booleanNode('failure')
212 9
                            ->info('Instantly re-queue the job on failure.')
213 9
                            ->defaultTrue()
214 9
                        ->end()
215 9
                        ->booleanNode('exception')
216 9
                            ->info('Instantly re-queue the job on exception.')
217 9
                            ->defaultFalse()
218 9
                        ->end()
219 9
                    ->end()
220 9
                ->end()
221 9
            ->end();
222
223 9
        return $rootNode;
224
    }
225
226 9
    protected function addPriority()
227
    {
228 9
        $treeBuilder = new TreeBuilder('priority');
229
230 9
        if (method_exists($treeBuilder, 'getRootNode')) {
231 9
            $rootNode = $treeBuilder->getRootNode();
232
        } else {
233
            // BC layer for symfony/config 4.1 and older
234
            $rootNode = $treeBuilder->root('priority');
235
        }
236
                $rootNode
237 9
            ->addDefaultsIfNotSet()
238 9
            ->children()
239 9
                ->integerNode('max')
240 9
                    ->defaultValue(255)
241 9
                    ->info('Maximum priority value.')
242 9
                    ->min(1)
243 9
                ->end()
244 9
                ->enumNode('direction')
245 9
                    ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])
246 9
                    ->info('Whether 1 is high priority or low prioirty.  '.PriorityJobManager::PRIORITY_ASC.' means 1 is low, '.PriorityJobManager::PRIORITY_DESC.' means 1 is high.')
247 9
                    ->defaultValue(PriorityJobManager::PRIORITY_DESC)
248 9
                ->end()
249 9
            ->end();
250
251 9
        return $rootNode;
252
    }
253
254 9
    protected function addClasses()
255
    {
256 9
        $treeBuilder = new TreeBuilder('class');
257
258 9
        if (method_exists($treeBuilder, 'getRootNode')) {
259 9
            $rootNode = $treeBuilder->getRootNode();
260
        } else {
261
            // BC layer for symfony/config 4.1 and older
262
            $rootNode = $treeBuilder->root('class');
263
        }
264
265
        $rootNode
266 9
            ->children()
267 9
                ->scalarNode('job')
268 9
                    ->info('If you want to override the Job class, put the class name here.')->end()
269 9
                ->scalarNode('job_archive')
270 9
                    ->info('If you want to override the JobArchive class, put the class name here.')->end()
271 9
                ->scalarNode('job_timing')
272 9
                    ->info('If you want to override the JobTiming class, put the class name here.')->end()
273 9
                ->scalarNode('run')
274 9
                    ->info('If you want to override the Run class, put the class name here.')->end()
275 9
                ->scalarNode('run_archive')
276 9
                    ->info('If you want to override the RunArchive class, put the class name here.')->end()
277 9
            ->end();
278
279 9
        return $rootNode;
280
    }
281
}
282