Completed
Push — master ( c7a8ed...7c53a3 )
by Florent
08:28
created

Configuration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 22
Bugs 9 Features 2
Metric Value
wmc 6
c 22
b 9
f 2
lcom 1
cbo 5
dl 30
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getConfigTreeBuilder() 0 21 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('compression_methods')
63
                    ->info('A list of enabled compression methods. Supported methods are: "DEF" (recommended), "GZ" and "ZLIB".')
64
                    ->prototype('scalar')
65
                    ->end()
66
                    ->treatNullLike([])
67
                ->end()
68
                ->end()
69
            ->end();
70
71
        return $treeBuilder;
72
    }
73
74
    /**
75
     * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition          $node
76
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
77
     */
78 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...
79
    {
80
        $sourceNodeBuilder = $node
81
            ->fixXmlConfig('source')
82
            ->children()
83
            ->arrayNode('keys')
84
            ->useAttributeAsKey('name')
85
            ->prototype('array')
86
            ->performNoDeepMerging()
87
            ->children();
88
        foreach ($jwk_sources as $name => $source) {
89
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
90
            $source->addConfiguration($sourceNode);
91
        }
92
    }
93
94
    /**
95
     * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition                $node
96
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] $jwk_set_sources
97
     */
98 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...
99
    {
100
        $sourceNodeBuilder = $node
101
            ->fixXmlConfig('source')
102
            ->children()
103
            ->arrayNode('key_sets')
104
            ->useAttributeAsKey('name')
105
            ->prototype('array')
106
            ->performNoDeepMerging()
107
            ->children();
108
        foreach ($jwk_set_sources as $name => $source) {
109
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
110
            $source->addConfiguration($sourceNode);
111
        }
112
    }
113
}
114