Completed
Push — master ( 86865c...653b85 )
by Kevin
26s queued 24s
created

getCustomErrorPagesControllerDeprecationMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0116
1
<?php
2
3
namespace Liip\MonitorBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\BaseNode;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * This class contains the configuration information for the bundle.
11
 *
12
 * This information is solely responsible for how the different configuration
13
 * sections are normalized, and merged.
14
 *
15
 * @author Lukas Kahwe Smith <[email protected]>
16
 */
17
class Configuration implements ConfigurationInterface
18
{
19
    /**
20
     * Generates the configuration tree.
21
     *
22
     * @return TreeBuilder
23
     */
24 49
    public function getConfigTreeBuilder()
25
    {
26 49
        $treeBuilder = new TreeBuilder('liip_monitor');
27
28
        // Keep compatibility with symfony/config < 4.2
29 49
        if (\method_exists($treeBuilder, 'getRootNode')) {
30 49
            $rootNode = $treeBuilder->getRootNode();
31
        } else {
32
            $rootNode = $treeBuilder->root('liip_monitor');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

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...
33
        }
34
35
        $rootNode
36 49
            ->beforeNormalization()
37
                ->always(function ($v) {
38 49
                    if (empty($v['default_group'])) {
39 49
                        $v['default_group'] = 'default';
40
                    }
41
42 49
                    if (isset($v['checks']) && is_array($v['checks']) && !isset($v['checks']['groups'])) {
43 40
                        $checks = $v['checks'];
44 40
                        unset($v['checks']);
45
46 40
                        $v['checks']['groups'][$v['default_group']] = $checks;
47
                    }
48
49 49
                    return $v;
50 49
                })
51 49
            ->end()
52 49
            ->children()
53 49
                ->booleanNode('enable_controller')->defaultFalse()->end()
54 49
                ->scalarNode('view_template')->defaultNull()->end()
55 49
                ->integerNode('failure_status_code')
56 49
                    ->min(100)->max(598)
57 49
                    ->defaultValue(502)
58 49
                ->end()
59 49
                ->arrayNode('mailer')
60 49
                    ->canBeEnabled()
61 49
                    ->children()
62 49
                        ->arrayNode('recipient')
63 49
                            ->isRequired()->requiresAtLeastOneElement()
64 49
                            ->prototype('scalar')->end()
65 49
                            ->beforeNormalization()
66 49
                                ->ifString()
67
                                ->then(function ($v) { return [$v]; })
68 49
                            ->end()
69 49
                        ->end()
70 49
                        ->scalarNode('sender')->isRequired()->cannotBeEmpty()->end()
71 49
                        ->scalarNode('subject')->isRequired()->cannotBeEmpty()->end()
72 49
                        ->booleanNode('send_on_warning')->defaultTrue()->end()
73 49
                    ->end()
74 49
                ->end()
75 49
                ->scalarNode('default_group')->defaultValue('default')->end()
76 49
                ->arrayNode('checks')
77 49
                    ->canBeUnset()
78 49
                    ->children()
79 49
                        ->append($this->createGroupsNode())
80 49
                    ->end()
81 49
                ->end()
82 49
            ->end()
83 49
        ->end();
84
85 49
        return $treeBuilder;
86
    }
87
88 49
    private function createGroupsNode()
89
    {
90 49
        $builder = new TreeBuilder('groups');
91
92
        // Keep compatibility with symfony/config < 4.2
93 49
        if (\method_exists($builder, 'getRootNode')) {
94 49
            $node = $builder->getRootNode();
95
        } else {
96
            $node = $builder->root('groups');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

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...
97
        }
98
        $node
99 49
            ->requiresAtLeastOneElement()
100 49
            ->info('Grouping checks')
101 49
            ->useAttributeAsKey('name')
102 49
            ->prototype('array')
103 49
                ->children()
104 49
                    ->arrayNode('php_extensions')
105 49
                        ->info('Validate that a named extension or a collection of extensions is available')
106 49
                        ->example('session.use_only_cookies: false')
107 49
                        ->prototype('scalar')->end()
108 49
                    ->end()
109 49
                    ->arrayNode('php_flags')
110 49
                        ->info('Pairs of a PHP setting and an expected value')
111 49
                        ->example('session.use_only_cookies: false')
112 49
                        ->useAttributeAsKey('setting')
113 49
                        ->prototype('scalar')->defaultValue(true)->end()
114 49
                    ->end()
115 49
                    ->arrayNode('php_version')
116 49
                        ->info('Pairs of a version and a comparison operator')
117 49
                        ->example('5.4.15: >=')
118 49
                        ->useAttributeAsKey('version')
119 49
                        ->prototype('scalar')->end()
120 49
                    ->end()
121 49
                    ->variableNode('process_running')
122 49
                        ->info('Process name/pid or an array of process names/pids')
123 49
                        ->example('[apache, foo]')
124 49
                    ->end()
125 49
                    ->arrayNode('readable_directory')
126 49
                        ->info('Validate that a given path (or a collection of paths) is a dir and is readable')
127 49
                        ->example('["%kernel.cache_dir%"]')
128 49
                        ->prototype('scalar')->end()
129 49
                    ->end()
130 49
                    ->arrayNode('writable_directory')
131 49
                        ->info('Validate that a given path (or a collection of paths) is a dir and is writable')
132 49
                        ->example('["%kernel.cache_dir%"]')
133 49
                        ->prototype('scalar')->end()
134 49
                    ->end()
135 49
                    ->arrayNode('class_exists')
136 49
                        ->info('Validate that a class or a collection of classes is available')
137 49
                        ->example('["Lua", "My\Fancy\Class"]')
138 49
                        ->prototype('scalar')->end()
139 49
                    ->end()
140 49
                    ->scalarNode('cpu_performance')
141 49
                        ->info('Benchmark CPU performance and return failure if it is below the given ratio')
142 49
                        ->example('1.0 # This is the power of an EC2 micro instance')
143 49
                    ->end()
144 49
                    ->arrayNode('disk_usage')
145 49
                        ->info('Checks to see if the disk usage is below warning/critical percent thresholds')
146 49
                        ->children()
147 49
                            ->integerNode('warning')->defaultValue(70)->end()
148 49
                            ->integerNode('critical')->defaultValue(90)->end()
149 49
                            ->scalarNode('path')->defaultValue('%kernel.cache_dir%')->end()
150 49
                        ->end()
151 49
                    ->end()
152 49
                    ->arrayNode('symfony_requirements')
153 49
                        ->info('Checks Symfony2 requirements file')
154 49
                        ->children()
155 49
                            ->scalarNode('file')->defaultValue('%kernel.root_dir%/SymfonyRequirements.php')->end()
156 49
                        ->end()
157 49
                    ->end()
158 49
                    ->arrayNode('opcache_memory')
159 49
                        ->info('Checks to see if the OpCache memory usage is below warning/critical thresholds')
160 49
                        ->children()
161 49
                            ->integerNode('warning')->defaultValue(70)->end()
162 49
                            ->integerNode('critical')->defaultValue(90)->end()
163 49
                        ->end()
164 49
                    ->end()
165 49
                    ->arrayNode('apc_memory')
166 49
                        ->info('Checks to see if the APC memory usage is below warning/critical thresholds')
167 49
                        ->children()
168 49
                            ->integerNode('warning')->defaultValue(70)->end()
169 49
                            ->integerNode('critical')->defaultValue(90)->end()
170 49
                        ->end()
171 49
                    ->end()
172 49
                    ->arrayNode('apc_fragmentation')
173 49
                        ->info('Checks to see if the APC fragmentation is below warning/critical thresholds')
174 49
                        ->children()
175 49
                            ->integerNode('warning')->defaultValue(70)->end()
176 49
                            ->integerNode('critical')->defaultValue(90)->end()
177 49
                        ->end()
178 49
                    ->end()
179 49
                    ->variableNode('doctrine_dbal')
180 49
                        ->defaultNull()
181 49
                        ->info('Connection name or an array of connection names')
182 49
                        ->example('[default, crm]')
183 49
                    ->end()
184 49
                    ->variableNode('doctrine_mongodb')
185 49
                        ->defaultNull()
186 49
                        ->info('Connection name or an array of connection names')
187 49
                        ->example('[default, crm]')
188 49
                    ->end()
189 49
                    ->arrayNode('doctrine_migrations')
190 49
                        ->useAttributeAsKey('name')
191 49
                        ->info('Checks to see if migrations from specified configuration file are applied')
192 49
                        ->prototype('array')
193 49
                            ->children()
194 49
                                ->scalarNode('configuration_file')
195 49
                                    ->info('Absolute path to doctrine migrations configuration')
196 49
                                ->end()
197 49
                                ->scalarNode('connection')
198 49
                                    ->isRequired()
199 49
                                    ->cannotBeEmpty()
200 49
                                    ->info('Connection name from doctrine DBAL configuration')
201 49
                                ->end()
202 49
                            ->end()
203 49
                            ->beforeNormalization()
204 49
                                ->ifString()
205
                                ->then(function ($value) {
206
                                    if (is_string($value)) {
207
                                        $value = ['connection' => $value];
208
                                    }
209
210
                                    return $value;
211 49
                                })
212 49
                            ->end()
213 49
                            ->validate()
214
                                ->always(function ($value) {
215
                                    if (is_array($value) && !isset($value['configuration_file']) && !class_exists('Doctrine\\Bundle\\MigrationsBundle\\Command\\DoctrineCommand')) {
216
                                        throw new \InvalidArgumentException('You should explicitly define "configuration_file" parameter or install doctrine/doctrine-migrations-bundle to use empty parameter.');
217
                                    }
218
219
                                    return $value;
220 49
                                })
221 49
                            ->end()
222 49
                        ->end()
223 49
                        ->example(
224
                            [
225
                                'application_migrations' => [
226 49
                                    'configuration_file' => '%kernel.root_dir%/Resources/config/migrations.yml',
227
                                    'connection' => 'default',
228
                                ],
229
                                'migrations_with_doctrine_bundle' => [
230
                                    'connection' => 'default',
231
                                ],
232
                                'migrations_with_doctrine_bundle_v2' => 'default',
233
                            ]
234
                        )
235 49
                    ->end()
236 49
                    ->arrayNode('memcache')
237 49
                        ->info('Check if MemCache extension is loaded and given server is reachable')
238 49
                        ->useAttributeAsKey('name')
239 49
                        ->prototype('array')
240 49
                            ->children()
241 49
                                ->scalarNode('host')->defaultValue('localhost')->end()
242 49
                                ->integerNode('port')->defaultValue(11211)->end()
243 49
                            ->end()
244 49
                        ->end()
245 49
                    ->end()
246 49
                    ->arrayNode('memcached')
247 49
                        ->info('Check if MemCached extension is loaded and given server is reachable')
248 49
                        ->useAttributeAsKey('name')
249 49
                        ->prototype('array')
250 49
                            ->children()
251 49
                                ->scalarNode('host')->defaultValue('localhost')->end()
252 49
                                ->integerNode('port')->defaultValue(11211)->end()
253 49
                            ->end()
254 49
                        ->end()
255 49
                    ->end()
256 49
                    ->arrayNode('redis')
257 49
                        ->info('Validate that a Redis service is running')
258 49
                        ->useAttributeAsKey('name')
259 49
                        ->beforeNormalization()
260 49
                            ->ifString()
261
                            ->then(function ($v) { return ['dsn' => $v]; })
262 49
                        ->end()
263 49
                        ->prototype('array')
264 49
                            ->children()
265 49
                                ->scalarNode('host')->defaultValue('localhost')->end()
266 49
                                ->integerNode('port')->defaultValue(6379)->end()
267 49
                                ->scalarNode('password')->defaultNull()->end()
268 49
                                ->scalarNode('dsn')->defaultNull()->end()
269 49
                            ->end()
270 49
                        ->end()
271 49
                    ->end()
272 49
                    ->arrayNode('http_service')
273 49
                        ->info('Attempt connection to given HTTP host and (optionally) check status code and page content')
274 49
                        ->useAttributeAsKey('name')
275 49
                        ->prototype('array')
276 49
                            ->children()
277 49
                                ->scalarNode('host')->defaultValue('localhost')->end()
278 49
                                ->integerNode('port')->defaultValue(80)->end()
279 49
                                ->scalarNode('path')->defaultValue('/')->end()
280 49
                                ->integerNode('status_code')->defaultValue(200)->end()
281 49
                                ->scalarNode('content')->defaultNull()->end()
282 49
                            ->end()
283 49
                        ->end()
284 49
                    ->end()
285 49
                    ->arrayNode('guzzle_http_service')
286 49
                        ->info('Attempt connection using Guzzle to given HTTP host and (optionally) check status code and page content')
287 49
                        ->useAttributeAsKey('name')
288 49
                        ->prototype('array')
289 49
                            ->children()
290 49
                                ->scalarNode('url')->defaultValue('localhost')->end()
291 49
                                ->variableNode('headers')->defaultValue([])->end()
292 49
                                ->variableNode('options')->defaultValue([])->end()
293 49
                                ->integerNode('status_code')->defaultValue(200)->end()
294 49
                                ->scalarNode('content')->defaultNull()->end()
295 49
                                ->scalarNode('method')->defaultValue('GET')->end()
296 49
                                ->scalarNode('body')->defaultNull()->end()
297 49
                            ->end()
298 49
                        ->end()
299 49
                    ->end()
300 49
                    ->arrayNode('rabbit_mq')
301 49
                        ->info('Validate that a RabbitMQ service is running')
302 49
                        ->useAttributeAsKey('name')
303 49
                        ->beforeNormalization()
304 49
                            ->ifString()
305
                            ->then(function ($v) { return ['dsn' => $v]; })
306 49
                        ->end()
307 49
                        ->prototype('array')
308 49
                            ->children()
309 49
                                ->scalarNode('host')->defaultValue('localhost')->end()
310 49
                                ->integerNode('port')->defaultValue(5672)->end()
311 49
                                ->scalarNode('user')->defaultValue('guest')->end()
312 49
                                ->scalarNode('password')->defaultValue('guest')->end()
313 49
                                ->scalarNode('vhost')->defaultValue('/')->end()
314 49
                                ->scalarNode('dsn')->defaultNull()->end()
315 49
                            ->end()
316 49
                        ->end()
317 49
                    ->end()
318 49
                    ->booleanNode('symfony_version')
319 49
                        ->info('Checks the version of this app against the latest stable release')
320 49
                    ->end()
321 49
                    ->arrayNode('custom_error_pages')
322 49
                        ->info('Checks if error pages have been customized for given error codes')
323 49
                        ->children()
324 49
                            ->arrayNode('error_codes')
325 49
                                ->example('[404, 503]')
326 49
                                ->info('The status codes that should be customized')
327 49
                                ->isRequired()
328 49
                                ->requiresAtLeastOneElement()
329 49
                                ->prototype('scalar')->end()
330 49
                            ->end()
331 49
                            ->scalarNode('path')
332 49
                                ->info('The directory where your custom error page twig templates are located. Keep as "%kernel.project_dir%" to use default location.')
333 49
                                ->defaultValue('%kernel.project_dir%')
334 49
                            ->end()
335 49
                            ->scalarNode('controller')
336 49
                                ->defaultNull()
337 49
                                ->setDeprecated(...self::getCustomErrorPagesControllerDeprecationMessage())
338 49
                            ->end()
339 49
                        ->end()
340 49
                    ->end()
341 49
                    ->arrayNode('security_advisory')
342 49
                        ->info('Checks installed composer dependencies against the SensioLabs Security Advisory database')
343 49
                        ->children()
344 49
                            ->scalarNode('lock_file')->defaultValue('%kernel.project_dir%/composer.lock')->end()
345 49
                        ->end()
346 49
                    ->end()
347 49
                    ->arrayNode('stream_wrapper_exists')
348 49
                        ->info('Validate that a stream wrapper or collection of stream wrappers exists')
349 49
                        ->example('[\'zlib\', \'bzip2\', \'zip\']')
350 49
                        ->prototype('scalar')->end()
351 49
                    ->end()
352 49
                    ->arrayNode('file_ini')
353 49
                        ->info('Find and validate INI files')
354 49
                        ->example('[\'path/to/my.ini\']')
355 49
                        ->prototype('scalar')->end()
356 49
                    ->end()
357 49
                    ->arrayNode('file_json')
358 49
                        ->info('Find and validate JSON files')
359 49
                        ->example('[\'path/to/my.json\']')
360 49
                        ->prototype('scalar')->end()
361 49
                    ->end()
362 49
                    ->arrayNode('file_xml')
363 49
                        ->info('Find and validate XML files')
364 49
                        ->example('[\'path/to/my.xml\']')
365 49
                        ->prototype('scalar')->end()
366 49
                    ->end()
367 49
                    ->arrayNode('file_yaml')
368 49
                        ->info('Find and validate YAML files')
369 49
                        ->example('[\'path/to/my.yml\']')
370 49
                        ->prototype('scalar')->end()
371 49
                    ->end()
372 49
                    ->arrayNode('pdo_connections')
373 49
                        ->info('PDO connections to check for connection')
374 49
                        ->useAttributeAsKey('name')
375 49
                        ->prototype('array')
376 49
                            ->children()
377 49
                                ->scalarNode('dsn')->defaultNull()->end()
378 49
                                ->scalarNode('username')->defaultNull()->end()
379 49
                                ->scalarNode('password')->defaultNull()->end()
380 49
                                ->integerNode('timeout')->defaultValue(1)->end()
381 49
                            ->end()
382 49
                        ->end()
383 49
                    ->end()
384 49
                    ->arrayNode('expressions')
385 49
                        ->useAttributeAsKey('alias')
386 49
                        ->info('Checks that fail/warn when given expression is false (expressions are evaluated with symfony/expression-language)')
387 49
                        ->example([
388
                            'opcache' => [
389 49
                                'label' => 'OPcache',
390
                                'warning_expression' => "ini('opcache.revalidate_freq') > 0",
391
                                'critical_expression' => "ini('opcache.enable')",
392
                                'warning_message' => 'OPcache not optimized for production',
393
                                'critical_message' => 'OPcache not enabled',
394
                            ],
395
                        ])
396 49
                        ->prototype('array')
397 49
                            ->addDefaultsIfNotSet()
398 49
                            ->validate()
399
                                ->ifTrue(function ($value) {
400 2
                                    return !$value['warning_expression'] && !$value['critical_expression'];
401 49
                                })
402 49
                                ->thenInvalid('A warning_expression or a critical_expression must be set.')
403 49
                            ->end()
404 49
                            ->children()
405 49
                                ->scalarNode('label')->isRequired()->end()
406 49
                                ->scalarNode('warning_expression')
407 49
                                ->defaultNull()
408 49
                                ->example('ini(\'apc.stat\') == 0')
409 49
                            ->end()
410 49
                            ->scalarNode('critical_expression')
411 49
                            ->defaultNull()
412 49
                            ->example('ini(\'short_open_tag\') == 1')
413 49
                        ->end()
414 49
                        ->scalarNode('warning_message')->defaultNull()->end()
415 49
                        ->scalarNode('critical_message')->defaultNull()->end()
416 49
                    ->end()
417 49
                ->end()
418 49
            ->end()
419
        ;
420
421 49
        return $node;
422
    }
423
424
    /**
425
     * Returns the correct deprecation param's as an array for setDeprecated.
426
     *
427
     * Symfony/Config v5.1 introduces a deprecation notice when calling
428
     * setDeprecation() with less than 3 args and the getDeprecation method was
429
     * introduced at the same time. By checking if getDeprecation() exists,
430
     * we can determine the correct param count to use when calling setDeprecated.
431
     */
432 49
    private static function getCustomErrorPagesControllerDeprecationMessage()
433
    {
434 49
        $message = 'The custom error page controller option is no longer used; the corresponding config parameter was deprecated in 2.13 and will be dropped in 3.0.';
435
436 49
        if (method_exists(BaseNode::class, 'getDeprecation')) {
437
            return [
438 49
                'liip/monitor-bundle',
439 49
                '2.13',
440 49
                $message,
441
            ];
442
        }
443
444
        return [$message];
445
    }
446
}
447