Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 79
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 79
c 0
b 0
f 0
rs 8.6545
cc 3
nc 1
nop 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 Norsys\SecurityBundle\DependencyInjection;
4
5
use function preg_match;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Symfony\Component\HttpFoundation\Request;
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
    public function getConfigTreeBuilder()
21
    {
22
        $treeBuilder = new TreeBuilder();
23
        $rootNode = $treeBuilder->root('norsys_security');
24
25
        $rootNode
26
            ->children()
27
                ->arrayNode('https_redirect')
28
                    ->info('Optional https redirect configuration')
29
                    ->addDefaultsIfNotSet()
30
                    ->children()
31
                        ->booleanNode('enabled')
32
                            ->info('Enable force redirect to https')
33
                            ->defaultValue(false)
34
                        ->end()
35
                    ->end()
36
                ->end()
37
                ->arrayNode('proxy')
38
                    ->info('Optional proxy configuration')
39
                    ->addDefaultsIfNotSet()
40
                    ->children()
41
                        ->booleanNode('enabled')
42
                            ->info('Enable proxy')
43
                            ->defaultValue(false)
44
                        ->end()
45
                        ->scalarNode('env_variable_name')
46
                            ->info('Configure name to environment variable list trusted proxies')
47
                            ->defaultValue('TRUSTED_PROXIES')
48
                        ->end()
49
                        ->scalarNode('env_variable_separator')
50
                            ->info('Configure proxy separator in environment variable listed trusted proxies')
51
                            ->defaultValue(',')
52
                        ->end()
53
                        ->scalarNode('trusted_header_set')
54
                            ->info('A bit field of Request::HEADER_*, to set which headers to trust from your proxies')
55
                            ->defaultValue(Request::HEADER_X_FORWARDED_ALL)
56
                            ->validate()
57
                                ->ifEmpty()
58
                                ->then(function () {
59
                                    return Request::HEADER_X_FORWARDED_ALL;
60
                                })
61
                            ->end()
62
                            ->validate()
63
                                ->ifTrue(
64
                                    function (string $header) {
65
                                        if (preg_match('/^HEADER_FORWARDED$|^HEADER_X_[\w]+$/', $header) === 0
66
                                            || defined(Request::class . '::' . $header) === false
67
                                        ) {
68
                                            return true;
69
                                        }
70
                                    }
71
                                )
72
                                ->thenInvalid('Invalid header to trust from your proxies %s')
73
                            ->end()
74
                        ->end()
75
                    ->end()
76
                ->end()
77
                ->arrayNode('coming_soon')
78
                    ->info('Optional coming soon configuration')
79
                    ->addDefaultsIfNotSet()
80
                    ->children()
81
                        ->booleanNode('enabled')
82
                            ->info('Redirect all requests to coming soon page')
83
                            ->defaultValue(false)
84
                        ->end()
85
                        ->scalarNode('template')
86
                            ->info('Template to display coming soon page')
87
                            ->defaultValue('NorsysSecurityBundle::coming_soon.html.twig')
88
                        ->end()
89
                        ->arrayNode('allowed_ips')
90
                            ->prototype('scalar')
91
                            ->end()
92
                            ->defaultValue([])
93
                        ->end()
94
                    ->end()
95
                ->end()
96
            ->end();
97
98
        return $treeBuilder;
99
    }
100
}
101