Completed
Push — master ( b9975d...05326a )
by Florent
06:18 queued 51s
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 51
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 18
Bugs 5 Features 1
Metric Value
c 18
b 5
f 1
dl 0
loc 51
rs 9.4109
cc 3
eloc 45
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
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
final class Configuration implements ConfigurationInterface
19
{
20
    /**
21
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[]
22
     */
23
    private $jwk_sources;
24
25
    /**
26
     * @var string
27
     */
28
    private $alias;
29
30
    /**
31
     * Configuration constructor.
32
     *
33
     * @param string                                                                    $alias
34
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
35
     */
36
    public function __construct($alias, array $jwk_sources)
37
    {
38
        $this->alias = $alias;
39
        $this->jwk_sources = $jwk_sources;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getConfigTreeBuilder()
46
    {
47
        $treeBuilder = new TreeBuilder();
48
        $rootNode = $treeBuilder->root($this->alias);
49
50
        $this->addJWKSourcesSection($rootNode, $this->jwk_sources);
51
52
        $rootNode
53
            ->children()
54
                ->arrayNode('compression_methods')
55
                    ->info('A list of enabled compression methods. Supported methods are: "DEF" (recommended), "GZ" and "ZLIB".')
56
                    ->prototype('scalar')
57
                    ->end()
58
                    ->treatNullLike([])
59
                ->end()
60
                ->arrayNode('key_sets')
61
                    ->defaultValue([])
62
                    ->useAttributeAsKey('key')
63
                    ->prototype('array')
64
                        ->prototype('scalar')
65
                        ->end()
66
                    ->end()
67
                ->end()
68
                ->arrayNode('storage')
69
                    ->addDefaultsIfNotSet()
70
                    ->validate()
71
                        ->ifTrue(function ($value) {
72
                            return true === $value['enabled'] && null !== $value['class'] && !class_exists($value['class']);
73
                        })
74
                        ->thenInvalid('The class does not exist')
75
                    ->end()
76
                    ->children()
77
                        ->booleanNode('enabled')
78
                            ->info('If true, the storage is used and "jti" header parameter is added.')
79
                            ->defaultFalse()
80
                        ->end()
81
                        ->scalarNode('manager')
82
                            ->info('The "jot" manager.')
83
                            ->defaultValue('jose.jot_manager.default')
84
                            ->cannotBeEmpty()
85
                        ->end()
86
                        ->scalarNode('class')
87
                            ->info('The "jot" class.')
88
                            ->defaultNull()
89
                        ->end()
90
                    ->end()
91
                ->end()
92
            ->end();
93
94
        return $treeBuilder;
95
    }
96
97
    /**
98
     * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition          $node
99
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
100
     */
101
    private function addJWKSourcesSection(ArrayNodeDefinition $node, array $jwk_sources)
102
    {
103
        $sourceNodeBuilder = $node
104
            ->fixXmlConfig('source')
105
            ->children()
106
            ->arrayNode('keys')
107
            ->useAttributeAsKey('name')
108
            ->prototype('array')
109
            ->performNoDeepMerging()
110
            ->children();
111
        foreach ($jwk_sources as $name => $source) {
112
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
113
            $source->addConfiguration($sourceNode);
114
        }
115
    }
116
}
117