Completed
Push — develop ( 17d645...b57b7f )
by Florent
02:19
created

Configuration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 160
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 25
Bugs 9 Features 3
Metric Value
wmc 6
c 25
b 9
f 3
lcom 1
cbo 5
dl 30
loc 160
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getConfigTreeBuilder() 0 85 1
A addJWKSourcesSection() 15 15 2
A addJWKSetSourcesSection() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[]
27
     */
28
    private $jwk_set_sources;
29
30
    /**
31
     * @var string
32
     */
33
    private $alias;
34
35
    /**
36
     * Configuration constructor.
37
     *
38
     * @param string                                                                          $alias
39
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[]       $jwk_sources
40
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] $jwk_set_sources
41
     */
42
    public function __construct($alias, array $jwk_sources, array $jwk_set_sources)
43
    {
44
        $this->alias = $alias;
45
        $this->jwk_sources = $jwk_sources;
46
        $this->jwk_set_sources = $jwk_set_sources;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getConfigTreeBuilder()
53
    {
54
        $treeBuilder = new TreeBuilder();
55
        $rootNode = $treeBuilder->root($this->alias);
56
57
        $this->addJWKSourcesSection($rootNode, $this->jwk_sources);
58
        $this->addJWKSetSourcesSection($rootNode, $this->jwk_set_sources);
59
60
        $rootNode
61
            ->children()
62
            ->arrayNode('jwt_loaders')
63
                ->useAttributeAsKey('name')
64
                ->prototype('array')
65
                    ->children()
66
                        ->scalarNode('verifier')->isRequired()->end()
67
                        ->scalarNode('checker')->isRequired()->end()
68
                        ->scalarNode('decrypter')->defaultNull()->end()
69
                        ->scalarNode('logger')->defaultNull()->end()
70
                    ->end()
71
                ->end()
72
            ->end()
73
            ->arrayNode('jwt_creators')
74
                ->useAttributeAsKey('name')
75
                ->prototype('array')
76
                    ->children()
77
                        ->scalarNode('signer')->isRequired()->end()
78
                        ->scalarNode('encrypter')->defaultNull()->end()
79
                    ->end()
80
                ->end()
81
            ->end()
82
            ->arrayNode('encrypters')
83
                ->useAttributeAsKey('name')
84
                ->prototype('array')
85
                    ->children()
86
                        ->arrayNode('key_encryption_algorithms')->isRequired()->prototype('scalar')->end()->end()
87
                        ->arrayNode('content_encryption_algorithms')->isRequired()->prototype('scalar')->end()->end()
88
                        ->arrayNode('compression_methods')->defaultValue(['DEF'])->prototype('scalar')->end()->end()
89
                        ->scalarNode('logger')->defaultNull()->end()
90
                        ->booleanNode('create_decrypter')->defaultTrue()->end()
91
                    ->end()
92
                ->end()
93
            ->end()
94
            ->arrayNode('decrypters')
95
                ->useAttributeAsKey('name')
96
                ->prototype('array')
97
                    ->children()
98
                        ->arrayNode('key_encryption_algorithms')->isRequired()->prototype('scalar')->end()->end()
99
                        ->arrayNode('content_encryption_algorithms')->isRequired()->prototype('scalar')->end()->end()
100
                        ->arrayNode('compression_methods')->defaultValue(['DEF'])->prototype('scalar')->end()->end()
101
                        ->scalarNode('logger')->defaultNull()->end()
102
                        ->booleanNode('create_decrypter')->defaultTrue()->end()
103
                    ->end()
104
                ->end()
105
            ->end()
106
            ->arrayNode('signers')
107
                ->useAttributeAsKey('name')
108
                ->prototype('array')
109
                    ->children()
110
                        ->arrayNode('algorithms')->isRequired()->prototype('scalar')->end()->end()
111
                        ->scalarNode('logger')->defaultNull()->end()
112
                        ->booleanNode('create_verifier')->defaultTrue()->end()
113
                    ->end()
114
                ->end()
115
            ->end()
116
            ->arrayNode('verifiers')
117
                ->useAttributeAsKey('name')
118
                ->prototype('array')
119
                    ->children()
120
                        ->arrayNode('algorithms')->isRequired()->prototype('scalar')->end()->end()
121
                        ->scalarNode('logger')->defaultNull()->end()
122
                    ->end()
123
                ->end()
124
            ->end()
125
            ->arrayNode('checkers')
126
                ->useAttributeAsKey('name')
127
                ->prototype('array')
128
                    ->children()
129
                        ->arrayNode('claims')->isRequired()->prototype('scalar')->end()->end()
130
                        ->arrayNode('headers')->isRequired()->prototype('scalar')->end()->end()
131
                    ->end()
132
                ->end()
133
            ->end();
134
135
        return $treeBuilder;
136
    }
137
138
    /**
139
     * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition          $node
140
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
141
     */
142 View Code Duplication
    private function addJWKSourcesSection(ArrayNodeDefinition $node, array $jwk_sources)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        $sourceNodeBuilder = $node
145
            ->fixXmlConfig('source')
146
            ->children()
147
            ->arrayNode('keys')
148
            ->useAttributeAsKey('name')
149
            ->prototype('array')
150
            ->performNoDeepMerging()
151
            ->children();
152
        foreach ($jwk_sources as $name => $source) {
153
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
154
            $source->addConfiguration($sourceNode);
155
        }
156
    }
157
158
    /**
159
     * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition                $node
160
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] $jwk_set_sources
161
     */
162 View Code Duplication
    private function addJWKSetSourcesSection(ArrayNodeDefinition $node, array $jwk_set_sources)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $sourceNodeBuilder = $node
165
            ->fixXmlConfig('source')
166
            ->children()
167
            ->arrayNode('key_sets')
168
            ->useAttributeAsKey('name')
169
            ->prototype('array')
170
            ->performNoDeepMerging()
171
            ->children();
172
        foreach ($jwk_set_sources as $name => $source) {
173
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
174
            $source->addConfiguration($sourceNode);
175
        }
176
    }
177
}
178