Failed Conditions
Push — ng ( 0fdecc...ff414d )
by Florent
03:42
created

GrantSource::getNodeDefinition()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 95
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 8.4117
c 0
b 0
f 0
cc 3
eloc 90
nc 1
nop 1

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
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Bundle\DependencyInjection\Component\Grant;
15
16
use OAuth2Framework\Bundle\DependencyInjection\Component\Component;
17
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType;
18
use OAuth2Framework\Component\TokenEndpoint\GrantType;
19
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
23
24
final class GrantSource implements Component
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function name(): string
30
    {
31
        return 'grant';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load(array $configs, ContainerBuilder $container)
38
    {
39
        $container->registerForAutoconfiguration(GrantType::class)->addTag('oauth2_server_grant_type');
40
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
41
42
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/grant'));
43
        $loader->load('grant.php');
44
45
        if ($configs['grant']['authorization_code']['enabled']) {
46
            $container->setParameter('oauth2_server.grant.authorization_code.min_length', $configs['grant']['authorization_code']['min_length']);
47
            $container->setParameter('oauth2_server.grant.authorization_code.max_length', $configs['grant']['authorization_code']['max_length']);
48
            $container->setParameter('oauth2_server.grant.authorization_code.lifetime', $configs['grant']['authorization_code']['lifetime']);
49
            $container->setParameter('oauth2_server.grant.authorization_code.enforce_pkce', $configs['grant']['authorization_code']['enforce_pkce']);
50
            $container->setAlias('oauth2_server.grant.authorization_code.repository', $configs['grant']['authorization_code']['repository']);
51
            $loader->load('authorization_code.php');
52
        }
53
54
        if ($configs['grant']['client_credentials']['enabled']) {
55
            $container->setParameter('oauth2_server.grant.client_credentials.issue_refresh_token', $configs['grant']['client_credentials']['issue_refresh_token']);
56
            $loader->load('client_credentials.php');
57
        }
58
59
        if ($configs['grant']['implicit']['enabled']) {
60
            $loader->load('implicit.php');
61
        }
62
63
        if ($configs['grant']['refresh_token']['enabled']) {
64
            $container->setParameter('oauth2_server.grant.refresh_token.min_length', $configs['grant']['refresh_token']['min_length']);
65
            $container->setParameter('oauth2_server.grant.refresh_token.max_length', $configs['grant']['refresh_token']['max_length']);
66
            $container->setParameter('oauth2_server.grant.refresh_token.lifetime', $configs['grant']['refresh_token']['lifetime']);
67
            $container->setAlias('oauth2_server.grant.refresh_token.repository', $configs['grant']['refresh_token']['repository']);
68
            $loader->load('refresh_token.php');
69
        }
70
71
        if ($configs['grant']['resource_owner_password_credential']['enabled']) {
72
            $loader->load('resource_owner_password_credential.php');
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getNodeDefinition(NodeDefinition $node)
80
    {
81
        $node->children()
82
            ->arrayNode($this->name())
83
                ->addDefaultsIfNotSet()
84
                ->children()
85
                    ->arrayNode('authorization_code')
86
                        ->validate()
87
                            ->ifTrue(function ($config) {
88
                                return $config['max_length'] < $config['min_length'];
89
                            })
90
                            ->thenInvalid('The option "max_length" must be greater than "min_length".')
91
                        ->end()
92
                        ->canBeEnabled()
93
                        ->children()
94
                            ->integerNode('min_length')
95
                                ->defaultValue(50)
96
                                ->min(0)
97
                                ->info('Minimum length of the randomly generated authorization code')
98
                            ->end()
99
                            ->integerNode('max_length')
100
                                ->defaultValue(100)
101
                                ->min(1)
102
                                ->info('Maximum length of the randomly generated authorization code')
103
                            ->end()
104
                            ->integerNode('lifetime')
105
                                ->defaultValue(30)
106
                                ->min(1)
107
                                ->info('Authorization code lifetime (in seconds)')
108
                            ->end()
109
                            ->scalarNode('repository')
110
                                ->isRequired()
111
                                ->info('The authorization code repository')
112
                            ->end()
113
                            ->booleanNode('enforce_pkce')
114
                                ->defaultFalse()
115
                                ->info('If true, the PKCE is required for all requests including the ones from confidential clients')
116
                            ->end()
117
                        ->end()
118
                    ->end()
119
                    ->arrayNode('client_credentials')
120
                        ->canBeEnabled()
121
                        ->children()
122
                            ->booleanNode('issue_refresh_token')
123
                                ->info('If enabled, a refresh token will be issued with an access token (not recommended)')
124
                                ->defaultFalse()
125
                            ->end()
126
                        ->end()
127
                    ->end()
128
                    ->arrayNode('resource_owner_password_credential')
129
                        ->canBeEnabled()
130
                    ->end()
131
                    ->arrayNode('implicit')
132
                        ->canBeEnabled()
133
                    ->end()
134
                    ->arrayNode('refresh_token')
135
                        ->canBeEnabled()
136
                        ->validate()
137
                            ->ifTrue(function ($config) {
138
                                return true === $config['enabled'] && empty($config['repository']);
139
                            })
140
                            ->thenInvalid('The option "repository" must be set.')
141
                        ->end()
142
                        ->validate()
143
                            ->ifTrue(function ($config) {
144
                                return true === $config['enabled'] && $config['max_length'] < $config['min_length'];
145
                            })
146
                            ->thenInvalid('The option "max_length" must be greater than "min_length".')
147
                        ->end()
148
                        ->children()
149
                            ->integerNode('min_length')
150
                                ->defaultValue(50)
151
                                ->min(0)
152
                                ->info('Minimum length of the randomly generated refresh tokens')
153
                            ->end()
154
                            ->integerNode('max_length')
155
                                ->defaultValue(100)
156
                                ->min(1)
157
                                ->info('Maximum length of the randomly generated refresh tokens')
158
                            ->end()
159
                            ->integerNode('lifetime')
160
                                ->defaultValue(60 * 60 * 24 * 7)
161
                                ->min(1)
162
                                ->info('The refresh token lifetime (in seconds)')
163
                            ->end()
164
                            ->scalarNode('repository')
165
                                ->defaultNull()
166
                                ->info('The refresh token repository')
167
                            ->end()
168
                        ->end()
169
                    ->end()
170
                ->end()
171
            ->end()
172
        ->end();
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function prepend(ContainerBuilder $container, array $config): array
179
    {
180
        //Nothing to do
181
        return [];
182
    }
183
}
184