Completed
Pull Request — master (#95)
by Hugo
09:46
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 69
CRAP Score 5.0005

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 69
cts 71
cp 0.9718
rs 8.0379
c 0
b 0
f 0
cc 5
nc 2
nop 0
crap 5.0005

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
namespace Algatux\InfluxDbBundle\DependencyInjection;
4
5
use Algatux\InfluxDbBundle\Events\Listeners\InfluxDbEventListener;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * This is the class that validates and merges configuration from your app/config files.
11
 *
12
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
13
 */
14
final class Configuration implements ConfigurationInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 11
    public function getConfigTreeBuilder()
20
    {
21 11
        if (method_exists(TreeBuilder::class, 'getRootNode')) {
22
            $treeBuilder = new TreeBuilder('influx_db');
23
            $rootNode = $treeBuilder->getRootNode();
24
        } else {
25 11
            $treeBuilder = new TreeBuilder();
0 ignored issues
show
Bug introduced by
The call to TreeBuilder::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
26 11
            $rootNode = $treeBuilder->root('influx_db');
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...
27
        }
28
29
        $rootNode
30 11
            ->beforeNormalization()
31
                ->ifTrue(function ($v) {
32 11
                    return is_array($v) && !array_key_exists('connections', $v);
33 11
                })
34
                ->then(function ($v) {
35 10
                    $excludedKeys = ['default_connection'];
36 10
                    $connection = [];
37 10
                    foreach ($v as $key => $value) {
38 8
                        if (in_array($key, $excludedKeys, true)) {
39 2
                            continue;
40
                        }
41 8
                        $connection[$key] = $v[$key];
42 8
                        unset($v[$key]);
43
                    }
44 10
                    $v['connections'] = ['default' => $connection];
45
46 10
                    return $v;
47 11
                })
48 11
            ->end()
49 11
            ->children()
50 11
                ->scalarNode('default_connection')->info('If not defined, the first connection will be taken.')->end()
51 11
                ->arrayNode('connections')
52 11
                    ->useAttributeAsKey('name')
53 11
                    ->prototype('array')
54 11
                        ->children()
55 11
                            ->scalarNode('host')
56 11
                                ->isRequired()
57 11
                                ->info('Your InfluxDB host address')
58 11
                            ->end()
59 11
                            ->scalarNode('database')
60 11
                                ->isRequired()
61 11
                                ->info('Your InfluxDB database name')
62 11
                            ->end()
63 11
                            ->booleanNode('udp')
64 11
                                ->defaultFalse()
65 11
                                ->info('Set it to true to activate the UDP connection')
66 11
                            ->end()
67 11
                            ->booleanNode('ssl')
68 11
                                ->defaultFalse()
69 11
                                ->info('Set it to true to enable SSL on HTTP connections (required for Influx Cloud)')
70 11
                            ->end()
71 11
                            ->booleanNode('ssl_verification')
72 11
                                ->defaultFalse()
73 11
                                ->info('Set it to true to activate ssl verification')
74 11
                            ->end()
75 11
                            ->integerNode('udp_port')->defaultValue(4444)->end()
76 11
                            ->integerNode('http_port')->defaultValue(8086)->end()
77 11
                            ->scalarNode('username')->defaultValue('')->end()
78 11
                            ->scalarNode('password')->defaultValue('')->end()
79 11
                            ->floatNode('timeout')
80 11
                                ->defaultValue(0.0)
81 11
                                ->info('Setup timeout (seconds) for your requests')
82 11
                            ->end()
83 11
                            ->floatNode('connect_timeout')
84 11
                                ->defaultValue(0.0)
85 11
                                ->info('Setup connection timeout (seconds) for your requests')
86 11
                            ->end()
87 11
                            ->booleanNode('listener_enabled')
88 11
                                ->defaultTrue()
89 11
                                ->info('Set it to false to disable the event listener configuration')
90 11
                            ->end()
91 11
                            ->scalarNode('listener_class')
92 11
                                ->defaultValue(InfluxDbEventListener::class)
93 11
                                ->info('Simple override for the default event listener class (constructor args and methods must match)')
94 11
                            ->end()
95 11
                        ->end()
96 11
                    ->end()
97 11
                ->end()
98 11
            ->end()
99
        ;
100
101 11
        return $treeBuilder;
102
    }
103
}
104