Passed
Push — master ( eb2b9c...2ce42f )
by Dāvis
05:18 queued 02:31
created

Openid::configure()   C

Complexity

Conditions 8
Paths 22

Size

Total Lines 38
Code Lines 28

Duplication

Lines 11
Ratio 28.95 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 22
nop 2
dl 11
loc 38
rs 5.3846
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 ExtensionInterface
12
{
13
    protected $alias;
14
15
    public function configure(ContainerBuilder &$container, $alias)
16
    {
17
        $this->alias = $alias.'.openid';
18
        $clientConfigurations = $container->getParameter($this->alias.'.clients');
19
        $clientServiceKeys = [];
20
        foreach ($clientConfigurations as $key => $clientConfig) {
21
            $tree = new TreeBuilder();
22
            $node = $tree->root('sludio_helper_openid_client/clients/'.$key);
23
            $this->buildClientConfiguration($node);
24
            $processor = new Processor();
25
            $config = $processor->process($tree->buildTree(), [$clientConfig]);
26
            $clientServiceKey = $this->alias.'.client.'.$key;
27
            $service = [
28
                'key' => $clientServiceKey,
29
            ];
30
            if (isset($config['provider_options']['name'])) {
31
                $service['name'] = $config['provider_options']['name'];
32
            } else {
33
                $service['name'] = ucfirst($key);
34
            }
35
36
            $clientServiceKeys[$key] = $service;
37 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...
38
                if ($ckey === 'provider_options') {
39
                    if (is_array($cvalue)) {
40
                        foreach ($cvalue as $pkey => $pvalue) {
41
                            $container->setParameter($clientServiceKey.'.option.'.$pkey, $pvalue);
42
                        }
43
                    }
44
                } else {
45
                    $container->setParameter($clientServiceKey.'.'.$ckey, $cvalue);
46
                }
47
            }
48
            $this->configureClient($container, $clientServiceKey);
49
        }
50
        $container->getDefinition($this->alias.'.registry')->replaceArgument(1, $clientServiceKeys);
51
        if ($container->getParameter($alias.'.oauth.enabled') === true) {
52
            $container->getDefinition($alias.'.registry')->replaceArgument(2, $clientServiceKeys);
53
        }
54
    }
55
56
    public function buildClientConfiguration(NodeDefinition &$node)
57
    {
58
        $optionsNode = $node->children();
59
60
        // @formatter:off
61
        $optionsNode
62
            ->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end()
63
            ->scalarNode('openid_url')->isRequired()->cannotBeEmpty()->end()
64
            ->scalarNode('preg_check')->isRequired()->cannotBeEmpty()->end()
65
            ->scalarNode('ns_mode')->defaultValue('sreg')->end()
66
            ->scalarNode('user_class')->isRequired()->end()
67
            ->scalarNode('user_provider')->defaultValue('Sludio\HelperBundle\Openid\Login\Login')->end()
68
            ->scalarNode('redirect_route')->isRequired()->cannotBeEmpty()->end()
69
            ->arrayNode('provider_options')->prototype('variable')->end()->end()
70
        ;
71
        // @formatter:on
72
73
        $optionsNode->end();
74
    }
75
76
    public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = [])
77
    {
78
        $clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider'));
79
        $clientDefinition->setArguments([
80
            $clientServiceKey,
81
            new Reference('request_stack'),
82
            new Reference('service_container'),
83
            new Reference('router'),
84
        ]);
85
    }
86
}
87