Configuration::getConfigTreeBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 42
c 3
b 0
f 0
dl 0
loc 54
ccs 43
cts 43
cp 1
rs 9.248
cc 1
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Artprima\PrometheusMetricsBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
/**
11
 * This is the class that validates and merges configuration from your app/config files.
12
 *
13
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
14
 */
15
class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 15
    public function getConfigTreeBuilder(): TreeBuilder
21
    {
22 15
        $treeBuilder = new TreeBuilder('artprima_prometheus_metrics');
23 15
        $rootNode = $treeBuilder->getRootNode();
24
25 15
        $supportedTypes = ['in_memory', 'apcu', 'redis'];
26
27
        $rootNode
28 15
            ->children()
29 15
                ->scalarNode('namespace')
30 15
                    ->isRequired()
31 15
                    ->cannotBeEmpty()
32 15
                ->end()
33 15
                ->scalarNode('type')
34 15
                    ->validate()
35 15
                        ->ifNotInArray($supportedTypes)
36 15
                        ->thenInvalid('The type %s is not supported. Please choose one of '.json_encode($supportedTypes))
37 15
                    ->end()
38 15
                    ->defaultValue('in_memory')
39 15
                    ->cannotBeEmpty()
40 15
                ->end()
41 15
                ->arrayNode('redis')
42 15
                    ->children()
43 15
                        ->scalarNode('host')->end()
44 15
                        ->integerNode('port')
45 15
                            ->defaultValue(6379)
46 15
                        ->end()
47 15
                        ->floatNode('timeout')->end()
48 15
                        ->floatNode('read_timeout')
49 15
                            ->validate()
50 15
                                ->always()
51
                                // here we force casting `float` to `string` to avoid TypeError when working with Redis
52
                                // see for more details: https://github.com/phpredis/phpredis/issues/1538
53 10
                                ->then(function ($v) {
54 9
                                    return (string) $v;
55 15
                                })
56 15
                            ->end()
57 15
                        ->end()
58 15
                        ->booleanNode('persistent_connections')->end()
59 15
                        ->scalarNode('password')->end()
60 15
                        ->integerNode('database')->end()
61 15
                    ->end()
62 15
                ->end()
63 15
                ->arrayNode('ignored_routes')
64 15
                    ->prototype('scalar')->end()
65 15
                    ->defaultValue(['prometheus_bundle_prometheus'])
66 15
                ->end()
67 15
            ->end();
68
69
        // Here you should define the parameters that are allowed to
70
        // configure your bundle. See the documentation linked above for
71
        // more information on that topic.
72
73 15
        return $treeBuilder;
74
    }
75
}
76