Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 1
nop 0
dl 0
loc 92
rs 7.2412
c 0
b 0
f 0

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 Netgen\Bundle\OpenWeatherMapBundle\DependencyInjection;
4
5
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\Configuration as SiteAccessConfiguration;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
8
/**
9
 * This is the class that validates and merges configuration from your app/config files.
10
 *
11
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12
 */
13
class Configuration extends SiteAccessConfiguration
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function getConfigTreeBuilder()
19
    {
20
        $treeBuilder = new TreeBuilder();
21
        $rootNode = $treeBuilder->root('netgen_open_weather_map');
22
23
        $builder = $this->generateScopeBaseNode($rootNode);
24
25
        $builder
26
            ->arrayNode('api_settings')
27
                ->children()
28
                    ->scalarNode('api_key')
29
                        ->cannotBeEmpty()
30
                        ->info('API key from OpenWeatherMap')
31
                    ->end()
32
                    ->scalarNode('units')
33
                        ->validate()
34
                            ->ifNotInArray(array(UnitsConstraints::IMPERIAL, UnitsConstraints::METRIC, UnitsConstraints::STANDARD))
35
                            ->thenInvalid('Invalid units parameter %s')
36
                        ->end()
37
                        ->info('Standard, metric, and imperial units are available')
38
                    ->end()
39
                    ->scalarNode('language')
40
                        ->cannotBeEmpty()
41
                        ->info('You can use lang parameter to get the output in your language')
42
                    ->end()
43
                    ->scalarNode('type')
44
                        ->validate()
45
                            ->ifNotInArray(array(SearchAccuracyConstraints::ACCURATE, SearchAccuracyConstraints::LIKE))
46
                            ->thenInvalid('Invalid search accuracy parameter %s')
47
                        ->end()
48
                        ->info('Search accuracy')
49
                    ->end()
50
                ->end()
51
            ->end();
52
53
        $builder
54
            ->arrayNode('cache_settings')
55
                ->validate()
56
                    ->ifTrue(function ($v) {
57
                        if (empty($v)) {
58
                            return true;
59
                        }
60
61
                        $requiredSettings = array();
62
63
                        switch ($v['handler']) {
64
                            case 'memcached':
65
                                $requiredSettings = array('ttl', 'server', 'port');
66
                                break;
67
                            case 'stash':
68
                                $requiredSettings = array('ttl');
69
                                break;
70
                            case 'null':
71
                                return false;
72
                        }
73
74
                        foreach ($requiredSettings as $setting) {
75
                            if (!array_key_exists($setting, $v)) {
76
                                return true;
77
                            }
78
                        }
79
80
                        return false;
81
                    })
82
                    ->thenInvalid('Invalid handler configuration')
83
                ->end()
84
                ->children()
85
                    ->scalarNode('handler')
86
                        ->cannotBeEmpty()
87
                        ->info('Cache handler')
88
                        ->validate()
89
                            ->ifNotInArray(array('stash', 'memcached', 'null'))
90
                            ->thenInvalid('Invalid cache handler %s')
91
                        ->end()
92
                    ->end()
93
                    ->scalarNode('ttl')
94
                        ->cannotBeEmpty()
95
                        ->info('Cache ttl in seconds')
96
                    ->end()
97
                    ->scalarNode('server')
98
                        ->cannotBeEmpty()
99
                        ->info('Memcached server IP address')
100
                    ->end()
101
                    ->scalarNode('port')
102
                        ->cannotBeEmpty()
103
                        ->info('Memcached server port')
104
                    ->end()
105
                ->end()
106
            ->end();
107
108
        return $treeBuilder;
109
    }
110
}
111