Passed
Branch master (c65ffc)
by Dāvis
03:08
created

Openidconnect::configure()   C

Complexity

Conditions 14
Paths 81

Size

Total Lines 60
Code Lines 44

Duplication

Lines 16
Ratio 26.67 %

Importance

Changes 0
Metric Value
cc 14
eloc 44
nc 81
nop 1
dl 16
loc 60
rs 6.325
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 Sludio\HelperBundle\DependencyInjection\Component;
4
5
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\Processor;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class Openidconnect implements Extensionable
12
{
13
    public function buildClientConfiguration(NodeDefinition &$node)
14
    {
15
        $optionsNode = $node->children();
16
17
        // @formatter:off
18
        $optionsNode
19
            ->scalarNode('client_key')->isRequired()->defaultNull()->end()
20
            ->scalarNode('client_secret')->defaultNull()->end()
21
            ->scalarNode('id_token_issuer')->isRequired()->defaultNull()->end()
22
            ->scalarNode('public_key')->isRequired()->cannotBeEmpty()->end()
23
            ->scalarNode('base_uri')->isRequired()->end()
24
            ->scalarNode('user_provider')->defaultValue('Sludio\HelperBundle\Openidconnect\Provider\OpenIDConnectProvider')->end()
25
            ->arrayNode('redirect')
26
                ->addDefaultsIfNotSet()
27
                ->children()
28
                    ->enumNode('type')
29
                        ->values(array('route', 'uri'))
30
                        ->defaultValue('route')
31
                    ->end()
32
                    ->scalarNode('route')->defaultNull()->end()
33
                    ->scalarNode('uri')->defaultNull()->end()
34
                    ->arrayNode('params')->prototype('variable')->end()->end()
35
                ->end()
36
            ->end()
37
            ->arrayNode('uris')
38
                ->prototype('array')
39
                    ->prototype('variable')->end()
40
                ->end()
41
            ->end()
42
        ;
43
        // @formatter:on
44
45
        $optionsNode->end();
46
    }
47
48
    private function buildUri(NodeDefinition &$node)
49
    {
50
        $optionsNode = $node->children();
51
52
        // @formatter:off
53
        $optionsNode
54
            ->arrayNode('params')->prototype('variable')->end()->end()
55
            ->arrayNode('url_params')->prototype('variable')->end()->end()
56
        ;
57
        // @formatter:on
58
59
        $optionsNode->end();
60
    }
61
62
    public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = [])
63
    {
64
        $clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider'));
65
        $clientDefinition->setArguments([
66
            $clientServiceKey,
67
            $container->getParameter($clientServiceKey),
68
            [],
69
            new Reference('router'),
70
        ]);
71
    }
72
73
    public function configure(ContainerBuilder &$container)
74
    {
75
        $clientConfigurations = $container->getParameter('sludio_helper.openidconnect.clients');
76
        $clientServiceKeys = [];
77
        foreach ($clientConfigurations as $key => $clientConfig) {
78
            $tree = new TreeBuilder();
79
            $processor = new Processor();
80
            $node = $tree->root('sludio_helper_openidconnect_client/clients/'.$key);
81
            $this->buildClientConfiguration($node);
82
            $config = $processor->process($tree->buildTree(), [$clientConfig]);
83
            $clientServiceKey = 'sludio_helper.openidconnect.client.'.$key;
84
            $container->setParameter($clientServiceKey, $clientConfig);
85
            $service = [
86
                'key' => $clientServiceKey,
87
            ];
88 View Code Duplication
            if (isset($config['options']) && isset($config['options']['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
                $service['name'] = $config['options']['name'];
90
            } else {
91
                $service['name'] = ucfirst($key);
92
            }
93
94
            $clientServiceKeys[$key] = $service;
95 View Code Duplication
            foreach ($config as $configKey => $configValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
                if ('options' === $configKey) {
97
                    if (is_array($configValue)) {
98
                        foreach ($configValue as $parameterKey => $parameterValue) {
99
                            $container->setParameter($clientServiceKey.'.option.'.$parameterKey, $parameterValue);
100
                        }
101
                    }
102
                } else {
103
                    $container->setParameter($clientServiceKey.'.'.$configKey, $configValue);
104
                }
105
            }
106
            $uriConfigurations = $container->getParameter('sludio_helper.openidconnect.client.'.$key.'.uris');
107
            foreach ($uriConfigurations as $subKey => $uriConfig) {
108
                $tree = new TreeBuilder();
109
                $processor = new Processor();
110
                $node = $tree->root('sludio_helper_openidconnect_client/clients/'.$key.'/uris/'.$subKey);
111
                $this->buildUri($node);
112
                $config = $processor->process($tree->buildTree(), [$uriConfig]);
113
                $params = [];
114
                foreach ($config as $subConfigKey => $subConfigValue) {
115
                    if ($subConfigKey === 'params') {
116
                        if (is_array($subConfigValue)) {
117
                            foreach ($subConfigValue as $subParameterKey => $subParameterValue) {
118
                                $params[$subParameterKey] = $subParameterValue;
119
                            }
120
                            if (!empty($params)) {
121
                                $params['client_id'] = $container->getParameter('sludio_helper.openidconnect.client.'.$key.'.client_key');
122
                                $container->setParameter($clientServiceKey.'.'.$subKey.'.'.$subConfigKey, $params);
123
                            }
124
                        }
125
                    } else {
126
                        $container->setParameter($clientServiceKey.'.'.$subKey.'.'.$subConfigKey, $subConfigValue);
127
                    }
128
                }
129
            }
130
            $this->configureClient($container, $clientServiceKey);
131
        }
132
        $container->getDefinition('sludio_helper.openidconnect.registry')->replaceArgument(1, $clientServiceKeys);
133
    }
134
}
135