GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( d1fd57...d85002 )
by Dmitri
01:47
created

Configuration::jwtNode()   C

Complexity

Conditions 11
Paths 1

Size

Total Lines 140
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 116
nc 1
nop 1
dl 0
loc 140
rs 5.8533
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
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
final class Configuration implements ConfigurationInterface
12
{
13
    public const SIGNER_SYMMETRIC = 'symmetric';
14
    public const SIGNER_ASYMMETRIC = 'asymmetric';
15
16
    private const SYMMETRIC_ALGOS = ['HS256', 'HS384', 'HS512'];
17
    private const ASYMMETRIC_ALGOS = ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512'];
18
19
    public const STORAGE_FIXED = 'fixed';
20
    public const STORAGE_REDIS = 'redis';
21
    public const STORAGE_DOCTRINE = 'doctrine';
22
23
    public function getConfigTreeBuilder(): TreeBuilder
24
    {
25
        $treeBuilder = new TreeBuilder();
26
27
        /** @var ArrayNodeDefinition $rootNode */
28
        $rootNode = $treeBuilder->root('damax_api_auth');
29
        $rootNode
30
            ->addDefaultsIfNotSet()
31
            ->children()
32
                ->append($this->apiKeyNode('api_key'))
33
                ->append($this->jwtNode('jwt'))
34
            ->end()
35
        ;
36
37
        return $treeBuilder;
38
    }
39
40
    private function apiKeyNode(string $name): ArrayNodeDefinition
41
    {
42
        return (new ArrayNodeDefinition($name))
43
            ->canBeEnabled()
44
            ->children()
45
                ->append($this->extractorsNode('extractors', [
46
                    [
47
                        'type' => 'header',
48
                        'name' => 'Authorization',
49
                        'prefix' => 'Token',
50
                    ],
51
                ]))
52
53
                ->enumNode('generator')
54
                    ->values(['fixed', 'random'])
55
                    ->defaultValue('random')
56
                ->end()
57
58
                ->arrayNode('storage')
1 ignored issue
show
Bug introduced by
The method arrayNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

58
                ->/** @scrutinizer ignore-call */ arrayNode('storage')
Loading history...
59
                    ->beforeNormalization()
60
                        ->ifTrue(function (array $config): bool {
61
                            return !isset($config[0]);
62
                        })
63
                        ->then(function (array $config): array {
64
                            return [
65
                                ['type' => self::STORAGE_FIXED, 'tokens' => $config],
66
                            ];
67
                        })
68
                    ->end()
69
                    ->arrayPrototype()
70
                        ->children()
71
                            ->enumNode('type')
72
                                ->isRequired()
73
                                ->values([self::STORAGE_FIXED, self::STORAGE_REDIS, self::STORAGE_DOCTRINE])
74
                            ->end()
75
                            ->arrayNode('tokens')
76
                                ->useAttributeAsKey(true)
77
                                ->requiresAtLeastOneElement()
78
                                ->scalarPrototype()
79
                                    ->isRequired()
80
                                ->end()
81
                            ->end()
82
                            ->booleanNode('writable')
83
                                ->defaultFalse()
84
                            ->end()
85
                            ->scalarNode('redis_client_id')
86
                                ->cannotBeEmpty()
87
                                ->defaultValue('snc_redis.default')
88
                            ->end()
89
                            ->scalarNode('key_prefix')
90
                                ->cannotBeEmpty()
91
                            ->end()
92
                            ->scalarNode('doctrine_connection_id')
93
                                ->cannotBeEmpty()
94
                                ->defaultValue('database_connection')
95
                            ->end()
96
                            ->scalarNode('table_name')
97
                                ->cannotBeEmpty()
98
                                ->defaultValue('api_key')
99
                            ->end()
100
                            ->arrayNode('fields')
101
                                ->children()
102
                                    ->scalarNode('key')->cannotBeEmpty()->end()
103
                                    ->scalarNode('ttl')->cannotBeEmpty()->end()
104
                                    ->scalarNode('identity')->cannotBeEmpty()->end()
105
                                ->end()
106
                            ->end()
107
                        ->end()
108
                    ->end()
109
                ->end()
110
            ->end()
111
        ;
112
    }
113
114
    private function jwtNode(string $name): ArrayNodeDefinition
115
    {
116
        return (new ArrayNodeDefinition($name))
117
            ->beforeNormalization()
118
                ->ifString()
119
                ->then(function (string $config): array {
120
                    return ['signer' => $config];
121
                })
122
            ->end()
123
            ->canBeEnabled()
1 ignored issue
show
Bug introduced by
The method canBeEnabled() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

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

123
            ->/** @scrutinizer ignore-call */ canBeEnabled()
Loading history...
124
            ->children()
125
                ->append($this->extractorsNode('extractors', [
126
                    [
127
                        'type' => 'header',
128
                        'name' => 'Authorization',
129
                        'prefix' => 'Bearer',
130
                    ],
131
                ]))
132
                ->scalarNode('identity_claim')
133
                    ->cannotBeEmpty()
134
                ->end()
135
                ->arrayNode('builder')
136
                    ->addDefaultsIfNotSet()
137
                    ->children()
138
                        ->scalarNode('issuer')
139
                            ->cannotBeEmpty()
140
                        ->end()
141
                        ->scalarNode('audience')
142
                            ->cannotBeEmpty()
143
                        ->end()
144
                        ->integerNode('ttl')
145
                            ->defaultValue(3600)
146
                        ->end()
147
                    ->end()
148
                ->end()
149
                ->arrayNode('parser')
150
                    ->children()
151
                        ->arrayNode('issuers')
152
                            ->requiresAtLeastOneElement()
153
                            ->scalarPrototype()
154
                                ->isRequired()
155
                            ->end()
156
                        ->end()
157
                        ->scalarNode('audience')
158
                            ->cannotBeEmpty()
159
                        ->end()
160
                    ->end()
161
                ->end()
162
                ->arrayNode('signer')
163
                    ->isRequired()
164
                    ->beforeNormalization()
165
                        ->ifString()
166
                        ->then(function (string $config): array {
167
                            return ['signing_key' => $config];
168
                        })
169
                    ->end()
170
                    ->beforeNormalization()
171
                        ->ifTrue(function (?array $config): bool {
172
                            $type = $config['type'] ?? self::SIGNER_SYMMETRIC;
173
174
                            return self::SIGNER_ASYMMETRIC === $type;
175
                        })
176
                        ->then(function (array $config): array {
177
                            if (isset($config['signing_key'])) {
178
                                $config['signing_key'] = 'file://' . $config['signing_key'];
179
                            }
180
181
                            if (isset($config['verification_key'])) {
182
                                $config['verification_key'] = 'file://' . $config['verification_key'];
183
                            }
184
185
                            if (!isset($config['algorithm'])) {
186
                                $config['algorithm'] = self::ASYMMETRIC_ALGOS[0];
187
                            }
188
189
                            return $config;
190
                        })
191
                    ->end()
192
                    ->validate()
193
                        ->ifTrue(function (array $config): bool {
194
                            return self::SIGNER_ASYMMETRIC === $config['type'] && empty($config['verification_key']);
195
                        })
196
                        ->thenInvalid('Verification key must be specified for "asymmetric" signer.')
197
                    ->end()
198
                    ->validate()
199
                        ->ifTrue(function (array $config): bool {
200
                            return self::SIGNER_SYMMETRIC === $config['type'] && !in_array($config['algorithm'], self::SYMMETRIC_ALGOS);
201
                        })
202
                        ->thenInvalid('HMAC algorithm must be specified for "symmetric" signer.')
203
                    ->end()
204
                    ->validate()
205
                        ->ifTrue(function (array $config): bool {
206
                            return self::SIGNER_ASYMMETRIC === $config['type'] && !in_array($config['algorithm'], self::ASYMMETRIC_ALGOS);
207
                        })
208
                        ->thenInvalid('RSA or ECDSA algorithm must be specified for "asymmetric" signer.')
209
                    ->end()
210
                    ->validate()
211
                        ->ifTrue(function (array $config): bool {
212
                            if (self::SIGNER_SYMMETRIC === $config['type']) {
213
                                return false;
214
                            }
215
216
                            return !is_readable($config['signing_key']) || !is_readable($config['verification_key']);
217
                        })
218
                        ->thenInvalid('Signing and/or verification key is not readable.')
219
                    ->end()
220
                    ->validate()
221
                        ->ifTrue(function (array $config): bool {
222
                            return self::SIGNER_SYMMETRIC === $config['type'] && !empty($config['verification_key']);
223
                        })
224
                        ->thenInvalid('Verification key must no be specified for "symmetric" signer.')
225
                    ->end()
226
                    ->validate()
227
                        ->ifTrue(function (array $config): bool {
228
                            return self::SIGNER_SYMMETRIC === $config['type'] && !empty($config['passphrase']);
229
                        })
230
                        ->thenInvalid('Passphrase must not be specified for "symmetric" signer.')
231
                    ->end()
232
                    ->children()
233
                        ->enumNode('type')
234
                            ->values(['symmetric', 'asymmetric'])
235
                            ->defaultValue('symmetric')
236
                        ->end()
237
                        ->enumNode('algorithm')
238
                            ->values(array_merge(self::SYMMETRIC_ALGOS, self::ASYMMETRIC_ALGOS))
239
                            ->defaultValue(self::SYMMETRIC_ALGOS[0])
240
                        ->end()
241
                        ->scalarNode('signing_key')
242
                            ->isRequired()
243
                        ->end()
244
                        ->scalarNode('verification_key')
245
                            ->cannotBeEmpty()
246
                        ->end()
247
                        ->scalarNode('passphrase')
248
                            ->cannotBeEmpty()
249
                            ->defaultValue('')
250
                        ->end()
251
                    ->end()
252
                ->end()
253
            ->end()
254
        ;
255
    }
256
257
    private function extractorsNode(string $name, array $defaults): ArrayNodeDefinition
258
    {
259
        return (new ArrayNodeDefinition($name))
260
            ->arrayPrototype()
261
                ->children()
262
                    ->enumNode('type')
263
                        ->isRequired()
264
                        ->values(['header', 'query', 'cookie'])
265
                    ->end()
266
                    ->scalarNode('name')
1 ignored issue
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

266
                    ->/** @scrutinizer ignore-call */ scalarNode('name')
Loading history...
267
                        ->isRequired()
268
                    ->end()
269
                    ->scalarNode('prefix')
270
                        ->cannotBeEmpty()
271
                    ->end()
272
                ->end()
273
            ->end()
274
            ->defaultValue($defaults)
275
        ;
276
    }
277
}
278