Failed Conditions
Pull Request — develop (#16)
by Denis
07:18
created

Configuration   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 90.2%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 53
c 3
b 0
f 0
dl 0
loc 75
ccs 46
cts 51
cp 0.902
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 70 3
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
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
10
11
/**
12
 * This is the class that validates and merges configuration from your app/config files.
13
 *
14
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
15
 */
16
class Configuration implements ConfigurationInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 6
    public function getConfigTreeBuilder()
22
    {
23 6
        $treeBuilder = new TreeBuilder('artprima_prometheus_metrics');
24 6
        $rootNode = $treeBuilder->getRootNode();
25
26 6
        $supportedTypes = ['in_memory', 'apcu', 'redis'];
27
28
        $rootNode
29 6
            ->children()
30 6
                ->scalarNode('namespace')
31 6
                    ->isRequired()
32 6
                    ->cannotBeEmpty()
33 6
                ->end()
34 6
                ->scalarNode('type')
35 6
                    ->validate()
36 6
                        ->ifNotInArray($supportedTypes)
37 6
                        ->thenInvalid('The type %s is not supported. Please choose one of '.json_encode($supportedTypes))
38 6
                    ->end()
39 6
                    ->defaultValue('in_memory')
40 6
                    ->cannotBeEmpty()
41 6
                ->end()
42 6
                ->arrayNode('redis')
43 6
                    ->children()
44 6
                        ->scalarNode('host')->end()
45 6
                        ->scalarNode('port')
46 6
                            ->validate()
47 6
                                ->always()
48
                                // here we force casting `float` to `string` to avoid TypeError when working with Redis
49
                                // see for more details: https://github.com/phpredis/phpredis/issues/1538
50
                                ->then(function ($v) {
51 3
                                    if ($v == null) {
52
                                        return null;
53
                                    }
54
55 3
                                    if (!is_int($v)) {
56
                                        $path = 'artprima_prometheus_metrics.redis.port';
57
                                        $ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected int, but got %s.', $path, gettype($v)));
58
                                        $ex->setPath($path);
59
                                        throw $ex;
60
                                    }
61
62 3
                                    return $v;
63 6
                                })
64 6
                            ->end()
65 6
                        ->end()
66 6
                        ->floatNode('timeout')->end()
67 6
                        ->floatNode('read_timeout')
68 6
                            ->validate()
69 6
                                ->always()
70
                                // here we force casting `float` to `string` to avoid TypeError when working with Redis
71
                                // see for more details: https://github.com/phpredis/phpredis/issues/1538
72
                                ->then(function ($v) { return (string) $v; } )
73 6
                            ->end()
74 6
                        ->end()
75 6
                        ->booleanNode('persistent_connections')->end()
76 6
                        ->scalarNode('password')->end()
77 6
                        ->integerNode('database')->end()
78 6
                    ->end()
79 6
                ->end()
80 6
                ->arrayNode('ignored_routes')
81 6
                    ->prototype('scalar')->end()
82 6
                    ->defaultValue(['prometheus_bundle_prometheus'])
83 6
                ->end()
84 6
            ->end();
85
86
        // Here you should define the parameters that are allowed to
87
        // configure your bundle. See the documentation linked above for
88
        // more information on that topic.
89
90 6
        return $treeBuilder;
91
    }
92
}
93