LoginCidadaoOpenIDExtension::process()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 45
nc 256
nop 1
dl 0
loc 66
ccs 0
cts 54
cp 0
crap 90
rs 6.4177
c 0
b 0
f 0

How to fix   Long Method   

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
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\DependencyInjection;
12
13
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
use Symfony\Component\DependencyInjection\Loader;
20
21
/**
22
 * This is the class that loads and manages your bundle configuration
23
 *
24
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
25
 */
26
class LoginCidadaoOpenIDExtension extends Extension implements ExtensionInterface, CompilerPassInterface
27
{
28
29
    /**
30
     * {@inheritdoc}
31
     * @throws \Exception
32
     */
33
    public function load(array $configs, ContainerBuilder $container)
34
    {
35
        $configuration = new Configuration();
36
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
37
38
        $loader = new Loader\YamlFileLoader($container,
39
            new FileLocator(__DIR__.'/../Resources/config'));
40
        $loader->load('services.yml');
41
    }
42
43
    public function process(ContainerBuilder $container)
44
    {
45
        $definition = $container->getDefinition('oauth2.server');
46
        $args = $definition->getArguments();
47
48
        // Use the array's key only when it's available
49
        $key = array_key_exists('oauth2.server.storage', $args) ? 'oauth2.server.storage' : 0;
50
51
        $args[$key]['user_claims'] = new Reference('oauth2.storage.user_claims');
52
        $args[$key]['public_key'] = new Reference('oauth2.storage.public_key');
53
54
        $definition->setArgument($key, $args[$key]);
55
56
        $responseTypes = [
57
            'token' => new Reference('oauth2.response_types.token'),
58
            'code' => new Reference('oauth2.response_types.code'),
59
            'id_token' => new Reference('oauth2.response_types.id_token'),
60
            'id_token token' => new Reference('oauth2.response_types.id_token_token'),
61
            'code id_token' => new Reference('oauth2.response_types.code_id_token'),
62
        ];
63
        foreach ($responseTypes as $key => $responseType) {
64
            $definition->addMethodCall('addResponseType', [$responseType, $key]);
65
        }
66
67
        if ($container->hasDefinition('gaufrette.jwks_fs_filesystem')) {
68
            $filesystem = new Reference('gaufrette.jwks_fs_filesystem');
69
            $fileName = $container->getParameter('jwks_private_key_file');
70
            $container->getDefinition('oauth2.storage.public_key')
71
                ->addMethodCall('setFilesystem', [$filesystem, $fileName]);
72
        }
73
74
        if ($container->hasDefinition('oauth2.grant_type.authorization_code')) {
75
            $sessionState = new Reference('oidc.storage.session_state');
76
            $container->getDefinition('oauth2.grant_type.authorization_code')
77
                ->addMethodCall('setSessionStateStorage', [$sessionState]);
78
        }
79
        if ($container->hasDefinition('oauth2.storage.authorization_code')) {
80
            $sessionState = new Reference('oidc.storage.session_state');
81
            $container->getDefinition('oauth2.storage.authorization_code')
82
                ->addMethodCall('setSessionStateStorage', [$sessionState]);
83
        }
84
85
        if ($container->hasDefinition('oauth2.scope_manager')) {
86
            $scopes = $container->getParameter('lc_supported_scopes');
87
            $container->getDefinition('oauth2.scope_manager')
88
                ->addMethodCall('setScopes', [$scopes]);
89
        }
90
91
        if ($container->hasDefinition('oauth2.storage.access_token')) {
92
            $subjectIdentifierService = $container->getDefinition('oidc.subject_identifier.service');
93
            $clientManagerService = $container->getDefinition('lc.client_manager');
94
95
            $container->getDefinition('oauth2.storage.access_token')
96
                ->addMethodCall('setSubjectIdentifierService', [$subjectIdentifierService])
97
                ->addMethodCall('setClientManager', [$clientManagerService]);
98
        }
99
100
        if ($container->hasDefinition('oauth2.response_types.id_token')) {
101
            $userManagerService = $container->getDefinition('lc.user_manager');
102
            $clientManagerService = $container->getDefinition('lc.client_manager');
103
            $subjectIdentifierService = $container->getDefinition('oidc.subject_identifier.service');
104
105
            $container->getDefinition('oauth2.response_types.id_token')
106
                ->addMethodCall('setUserManager', [$userManagerService])
107
                ->addMethodCall('setClientManager', [$clientManagerService])
108
                ->addMethodCall('setSubjectIdentifierService', [$subjectIdentifierService]);
109
        }
110
    }
111
}
112