Passed
Branch master (a0cc06)
by Dāvis
17:06
created

Openid::configure()   D

Complexity

Conditions 9
Paths 22

Size

Total Lines 36
Code Lines 26

Duplication

Lines 16
Ratio 44.44 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 22
nop 1
dl 16
loc 36
rs 4.909
c 0
b 0
f 0
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 Openid implements Extensionable
12
{
13
    public function configure(ContainerBuilder &$container)
14
    {
15
        $clientConfigurations = $container->getParameter('sludio_helper.openid.clients');
16
        foreach ($clientConfigurations as $key => $clientConfig) {
17
            $tree = new TreeBuilder();
18
            $node = $tree->root('sludio_helper_openid_client/clients/'.$key);
19
            $this->buildClientConfiguration($node);
20
            $processor = new Processor();
21
            $config = $processor->process($tree->buildTree(), [$clientConfig]);
22
            $clientServiceKey = 'sludio_helper.openid.client.'.$key;
23
            $service = [
24
                'key' => $clientServiceKey,
25
            ];
26 View Code Duplication
            if (isset($config['provider_options']) && isset($config['provider_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...
27
                $service['name'] = $config['provider_options']['name'];
28
            } else {
29
                $service['name'] = ucfirst($key);
30
            }
31
32
            $clientServiceKeys[$key] = $service;
33 View Code Duplication
            foreach ($config as $ckey => $cvalue) {
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...
34
                if ($ckey === 'provider_options') {
35
                    if (is_array($cvalue)) {
36
                        foreach ($cvalue as $pkey => $pvalue) {
37
                            $container->setParameter($clientServiceKey.'.option.'.$pkey, $pvalue);
38
                        }
39
                    }
40
                } else {
41
                    $container->setParameter($clientServiceKey.'.'.$ckey, $cvalue);
42
                }
43
            }
44
            $this->configureClient($container, $clientServiceKey);
45
        }
46
        $container->getDefinition('sludio_helper.openid.registry')->replaceArgument(1, $clientServiceKeys);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $clientServiceKeys seems to be defined by a foreach iteration on line 16. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
47
        if ($container->getParameter('sludio_helper.oauth.enabled', false)) {
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Depend...ntainer::getParameter() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        if (/** @scrutinizer ignore-call */ $container->getParameter('sludio_helper.oauth.enabled', false)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
48
            $container->getDefinition('sludio_helper.registry')->replaceArgument(2, $clientServiceKeys);
49
        }
50
    }
51
52
    public function buildClientConfiguration(NodeDefinition &$node)
53
    {
54
        $optionsNode = $node->children();
55
56
        // @formatter:off
57
        $optionsNode
58
            ->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end()
59
            ->scalarNode('openid_url')->isRequired()->cannotBeEmpty()->end()
60
            ->scalarNode('preg_check')->isRequired()->cannotBeEmpty()->end()
61
            ->scalarNode('ns_mode')->defaultValue('sreg')->end()
62
            ->scalarNode('user_class')->isRequired()->end()
63
            ->scalarNode('user_provider')->defaultValue('Sludio\HelperBundle\Openid\Login\Login')->end()
64
            ->scalarNode('redirect_route')->isRequired()->cannotBeEmpty()->end()
65
            ->arrayNode('provider_options')->prototype('variable')->end()->end()
66
        ;
67
        // @formatter:on
68
69
        $optionsNode->end();
70
    }
71
72
    public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = [])
73
    {
74
        $clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider'));
75
        $clientDefinition->setArguments([
76
            $clientServiceKey,
77
            new Reference('request_stack'),
78
            new Reference('service_container'),
79
            new Reference('router'),
80
        ]);
81
    }
82
}
83