Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

Configuration::configureGraphiQL()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 84
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 57
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 57
cts 57
cp 1
rs 8.7169
c 0
b 0
f 0
cc 1
eloc 59
nc 1
nop 1
crap 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
/*******************************************************************************
4
 *  This file is part of the GraphQL Bundle package.
5
 *
6
 *  (c) YnloUltratech <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 ******************************************************************************/
11
12
namespace Ynlo\GraphQLBundle\DependencyInjection;
13
14
use GraphQL\Validator\Rules\QueryComplexity;
15
use GraphQL\Validator\Rules\QueryDepth;
16
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
use Ynlo\GraphQLBundle\Encoder\SecureIDEncoder;
20
use Ynlo\GraphQLBundle\Error\DefaultErrorFormatter;
21
use Ynlo\GraphQLBundle\Error\DefaultErrorHandler;
22
23
/**
24
 * Class Configuration
25
 */
26
class Configuration implements ConfigurationInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function getConfigTreeBuilder()
32
    {
33 1
        $treeBuilder = new TreeBuilder();
34
        /** @var NodeBuilder $rootNode */
35 1
        $rootNode = $treeBuilder->root('graphql')->addDefaultsIfNotSet()->children();
0 ignored issues
show
Bug introduced by
The method addDefaultsIfNotSet() 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

35
        $rootNode = $treeBuilder->root('graphql')->/** @scrutinizer ignore-call */ addDefaultsIfNotSet()->children();
Loading history...
36 1
        $this->configureEndpoints($rootNode);
37 1
        $this->configureErrorHandling($rootNode);
38 1
        $this->configureCORS($rootNode);
39 1
        $this->configureGraphiQL($rootNode);
40 1
        $this->configurePlugins($rootNode);
41 1
        $this->configureSecurity($rootNode);
42 1
        $this->configureOthers($rootNode);
43
44 1
        return $treeBuilder;
45
    }
46
47 1
    protected function configureErrorHandling(NodeBuilder $root)
48
    {
49 1
        $errorHandling = $root->arrayNode('error_handling')
50 1
                              ->info('It is important to handle errors and when possible, report these errors back to your users for information. ')
51 1
                              ->addDefaultsIfNotSet()
52 1
                              ->children();
53
54 1
        $errorHandling->enumNode('validation_messages')
55 1
                      ->values(['error', 'payload', 'both'])
56 1
                      ->info('Where should be displayed validation messages.')
57 1
                      ->defaultValue('error');
58
59 1
        $errorHandling->booleanNode('show_trace')->info('Show error trace in debug mode')->defaultTrue();
60
61 1
        $errorHandling->scalarNode('formatter')
62 1
                      ->info('Formatter is responsible for converting instances of Error to an array')
63 1
                      ->defaultValue(DefaultErrorFormatter::class);
64
65 1
        $errorHandling->scalarNode('handler')
66 1
                      ->info('Handler is useful for error filtering and logging.')
67 1
                      ->defaultValue(DefaultErrorHandler::class);
68
69
        $controlledErrors = $errorHandling
70 1
            ->arrayNode('controlled_errors')
71 1
            ->info('List of controlled errors')
72 1
            ->addDefaultsIfNotSet()
73 1
            ->children();
74
75 1
        $map = $controlledErrors->arrayNode('map')->useAttributeAsKey('code')->arrayPrototype()->children();
76 1
        $map->scalarNode('message')->isRequired();
77 1
        $map->scalarNode('description')->isRequired();
78 1
        $map->scalarNode('category')->defaultValue('user');
79
80
        $autoload = $controlledErrors
81 1
            ->arrayNode('autoload')
82 1
            ->info('Autoload exceptions implementing ControlledErrorInterface')
83 1
            ->addDefaultsIfNotSet()
84 1
            ->canBeDisabled()
85 1
            ->children();
86
87
        $autoload
88 1
            ->variableNode('locations')
89 1
            ->defaultValue(['Exception', 'Error'])
90 1
            ->info('Default folder to find exceptions and errors implementing controlled interface.')
91 1
            ->beforeNormalization()
92 1
            ->ifString()
93 1
            ->then(
94 1
                function ($v) {
95
                    return [$v];
96 1
                }
97
            )
98 1
            ->end();
99
100
        $autoload
101 1
            ->variableNode('whitelist')
102 1
            ->info('White listed classes')
103 1
            ->defaultValue(['/App\\\\[Exception|Error]/', '/\w+Bundle\\\\[Exception|Error]/'])
104 1
            ->beforeNormalization()
105 1
            ->ifString()
106 1
            ->then(
107 1
                function ($v) {
108
                    return [$v];
109 1
                }
110
            )
111 1
            ->end()
112 1
            ->validate()
113 1
            ->ifTrue(
114 1
                function (array $value) {
115
                    foreach ($value as $val) {
116
                        try {
117
                            preg_match($val, null);
118
                        } catch (\Exception $exception) {
119
                            return true;
120
                        }
121
                    }
122 1
                }
123 1
            )->thenInvalid('Invalid regular expression');
124
125
        $autoload
126 1
            ->variableNode('blacklist')
127 1
            ->info('Black listed classes')
128 1
            ->beforeNormalization()
129 1
            ->ifString()
130 1
            ->then(
131 1
                function ($v) {
132
                    return [$v];
133 1
                }
134
            )
135 1
            ->end()
136 1
            ->validate()
137 1
            ->ifTrue(
138 1
                function (array $value) {
139
                    foreach ($value as $val) {
140
                        try {
141
                            preg_match($val, null);
142
                        } catch (\Exception $exception) {
143
                            return true;
144
                        }
145
                    }
146 1
                }
147 1
            )->thenInvalid('Invalid regular expression');
148 1
    }
149
150 1
    protected function configureEndpoints(NodeBuilder $root)
151
    {
152 1
        $endpoints = $root->arrayNode('endpoints')
153 1
                          ->useAttributeAsKey('name')
154 1
                          ->validate()
155 1
                          ->ifTrue(
156 1
                              function ($v) {
157 1
                                  return array_key_exists('default', $v);
158 1
                              }
159 1
                          )->thenInvalid('"default" can\'t be used as endpoint name, the system internally use this endpoint name to store the entire schema.')
160 1
                          ->end()
161 1
                          ->arrayPrototype()
0 ignored issues
show
Bug introduced by
The method arrayPrototype() 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

161
                          ->/** @scrutinizer ignore-call */ arrayPrototype()
Loading history...
162 1
                          ->children();
163
164 1
        $endpoints->arrayNode('roles')
165 1
                  ->beforeNormalization()
166 1
                  ->ifString()
167 1
                  ->then(
168 1
                      function ($v) {
169 1
                          return preg_split('/\s*,\s*/', $v);
170 1
                      }
171
                  )
172 1
                  ->end()
173 1
                  ->prototype('scalar')
174 1
                  ->end();
175
176 1
        $endpoints->scalarNode('host')->example('^api\.backend\.');
177 1
        $endpoints->scalarNode('path')->example('/backend');
178
179 1
        $root->arrayNode('endpoint_alias')
180 1
             ->info('Use alias to refer to multiple endpoints using only one name')
181 1
             ->useAttributeAsKey('name')
182 1
             ->beforeNormalization()
183 1
             ->ifString()
184 1
             ->then(
185 1
                 function ($v) {
186
                     return preg_split('/\s*,\s*/', $v);
187 1
                 }
188
             )
189 1
             ->end()
190 1
             ->variablePrototype();
0 ignored issues
show
Bug introduced by
The method variablePrototype() 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

190
             ->/** @scrutinizer ignore-call */ variablePrototype();
Loading history...
191
192 1
        $root->scalarNode('endpoint_default')->info('Endpoint to apply to all definitions without explicit endpoint.');
193
194 1
    }
195
196 1
    protected function configureGraphiQL(NodeBuilder $root)
197
    {
198 1
        $graphiql = $root->arrayNode('graphiql')->addDefaultsIfNotSet()->children();
199
200 1
        $graphiql->scalarNode('title')
201 1
                 ->defaultValue('GraphQL API Explorer');
202
203
        $graphiql
204 1
            ->scalarNode('data_warning_message')
205 1
            ->defaultValue('Heads up! GraphQL Explorer makes use of your <strong>real</strong>, <strong>live</strong>, <strong>production</strong> data.');
206 1
        $graphiql->booleanNode('data_warning_dismissible')->defaultTrue();
207 1
        $graphiql->enumNode('data_warning_style')->values(['info', 'warning', 'danger'])->defaultValue('danger');
208
209 1
        $graphiql->scalarNode('template')->defaultValue('@YnloGraphQL/explorer.html.twig');
210 1
        $graphiql->scalarNode('default_query')->defaultNull()->info('An optional GraphQL string to use when no query exists from a previous session. If none is provided, GraphiQL will use its own default query.');
211
212 1
        $graphiql->scalarNode('favicon')->info('Url or path to favicon');
213
214 1
        $docs = $graphiql->arrayNode('documentation')->info('Display external API documentation link')->addDefaultsIfNotSet()->children();
215 1
        $docs->scalarNode('link')->info('Url, route or path.');
216 1
        $docs->scalarNode('btn_label')->defaultValue('Documentation');
217 1
        $docs->scalarNode('btn_class')->defaultValue('btn btn-outline-success');
218
219 1
        $authentication = $graphiql->arrayNode('authentication')->addDefaultsIfNotSet()->children();
220
        $authentication
221 1
            ->booleanNode('required')
222 1
            ->info(
223 1
                'The API require credentials to make any requests, 
224
if this value is FALSE and a provider is specified the authentication is optional.'
225
            )
226 1
            ->defaultFalse();
227
228 1
        $authentication->scalarNode('login_message')
229 1
                       ->defaultValue('Start exploring GraphQL API queries using your account’s data now.');
230
231 1
        $authenticationProvider = $authentication->arrayNode('provider')->children();
232
233 1
        $jwt = $authenticationProvider->arrayNode('jwt')->canBeEnabled()->children();
234
235 1
        $jwtLogin = $jwt->arrayNode('login')->children();
236
237 1
        $jwtLogin->scalarNode('url')
238 1
                 ->info('Route name or URI to make the login process to retrieve the token.')
239 1
                 ->isRequired();
240
241 1
        $jwtLogin->scalarNode('username_parameter')
242 1
                 ->defaultValue('username');
243
244 1
        $jwtLogin->scalarNode('username_label')
245 1
                 ->defaultValue('Username');
246
247 1
        $jwtLogin->scalarNode('password_parameter')
248 1
                 ->defaultValue('password');
249
250 1
        $jwtLogin->scalarNode('password_label')
251 1
                 ->defaultValue('Password');
252
253 1
        $jwtLogin->enumNode('parameters_in')
254 1
                 ->values(['form', 'query', 'header'])
255 1
                 ->info('How pass parameters to request the token')
256 1
                 ->defaultValue('form');
257
258 1
        $jwtLogin->scalarNode('response_token_path')
259 1
                 ->defaultValue('token')
260 1
                 ->info('Where the token should be located in the response in case of JSON, set null if the response is the token.');
261
262 1
        $jwtRequests = $jwt->arrayNode('requests')->addDefaultsIfNotSet()->children();
263
264 1
        $jwtRequests->enumNode('token_in')
265 1
                    ->values(['query', 'header'])
266 1
                    ->info('Where should be located the token on every request')
267 1
                    ->defaultValue('header');
268
269 1
        $jwtRequests->scalarNode('token_name')
270 1
                    ->defaultValue('Authorization')
271 1
                    ->info('Name of the token in query or header name');
272
273 1
        $jwtRequests->scalarNode('token_template')
274 1
                    ->defaultValue('Bearer {token}')
275 1
                    ->info('Customize how the token should be send,  use the place holder {token} to replace for current token');
276
277 1
        $authenticationProvider->scalarNode('custom')
278 1
                               ->defaultNull()
279 1
                               ->info('Configure custom service to use as authentication provider');
280 1
    }
281
282 1
    protected function configureCORS(NodeBuilder $root)
283
    {
284 1
        $cors = $root->arrayNode('cors')->canBeEnabled()->children();
285 1
        $cors->booleanNode('allow_credentials')->defaultTrue();
286 1
        $cors->variableNode('allow_headers')->defaultValue(['Origin', 'Content-Type', 'Accept', 'Authorization']);
287 1
        $cors->integerNode('max_age')->defaultValue(3600);
288 1
        $cors->variableNode('allow_methods')->defaultValue(['POST', 'GET', 'OPTIONS']);
289 1
        $cors->variableNode('allow_origins')->defaultValue(['*']);
290 1
    }
291
292 1
    protected function configurePlugins(NodeBuilder $root)
293
    {
294 1
        $this->configurePluginPaginationGlobalConfig($root);
295 1
        $this->configurePluginNamespaceGlobalConfig($root);
296 1
    }
297
298 1
    protected function configurePluginPaginationGlobalConfig(NodeBuilder $root)
299
    {
300 1
        $pagination = $root->arrayNode('pagination')->addDefaultsIfNotSet()->children();
301 1
        $pagination->integerNode('limit')
302 1
                   ->defaultValue(100)->info('Maximum limit allowed for all paginations');
303 1
    }
304
305 1
    protected function configurePluginNamespaceGlobalConfig(NodeBuilder $root)
306
    {
307 1
        $namespaces = $root->arrayNode('namespaces')
308 1
                           ->info(
309 1
                               'Group GraphQL schema using namespaced schemas. 
310
On large schemas is  helpful to keep schemas grouped by bundle and node'
311
                           )
312 1
                           ->canBeEnabled()
313 1
                           ->addDefaultsIfNotSet()
314 1
                           ->children();
315
316 1
        $bundles = $namespaces->arrayNode('bundles')
317 1
                              ->info('Group each bundle into a separate schema definition')
318 1
                              ->canBeDisabled()
319 1
                              ->addDefaultsIfNotSet()
320 1
                              ->children();
321
322 1
        $bundles->scalarNode('query_suffix')
323 1
                ->info('The following suffix will be used for bundle query groups')
324 1
                ->defaultValue('BundleQuery');
325
326 1
        $bundles->scalarNode('mutation_suffix')
327 1
                ->info('The following suffix will be used for bundle mutation groups')
328 1
                ->defaultValue('BundleMutation');
329
330 1
        $bundles->variableNode('ignore')
331 1
                ->info('The following bundles will be ignore for grouping, all definitions will be placed in the root query or mutation')
332 1
                ->defaultValue(['AppBundle']);
333
334 1
        $bundles->arrayNode('aliases')
335 1
                ->info(
336 1
                    'Define aliases for bundles to set definitions inside other desired bundle name. 
337
Can be used to group multiple bundles or publish a bundle with a different name'
338
                )
339 1
                ->example('SecurityBundle: AppBundle')
340 1
                ->useAttributeAsKey('name')
341 1
                ->prototype('scalar');
342
343
344 1
        $nodes = $namespaces->arrayNode('nodes')
345 1
                            ->info('Group queries and mutations of the same node into a node specific schema definition.')
346 1
                            ->addDefaultsIfNotSet()
347 1
                            ->canBeDisabled()
348 1
                            ->children();
349
350 1
        $nodes->scalarNode('query_suffix')
351 1
              ->info('The following suffix will be used to create the name for queries to the same node')
352 1
              ->defaultValue('Query');
353
354 1
        $nodes->scalarNode('mutation_suffix')
355 1
              ->info('The following suffix will be used to create the name for mutations to the same node')
356 1
              ->defaultValue('Mutation');
357
358 1
        $nodes->variableNode('ignore')
359 1
              ->info('The following nodes will be ignore for grouping, all definitions will be placed in the root query or mutation')
360 1
              ->defaultValue(['Node']);
361
362 1
        $nodes->arrayNode('aliases')
363 1
              ->info(
364 1
                  'Define aliases for nodes to set definitions inside other desired node name. 
365
Can be used to group multiple nodes or publish a node with a different group name'
366
              )
367 1
              ->example('InvoiceItem: Invoice')
368 1
              ->useAttributeAsKey('name')
369 1
              ->prototype('scalar');
370 1
    }
371
372 1
    private function configureSecurity(NodeBuilder $rootNode)
373
    {
374
        $securityNode = $rootNode
375 1
            ->arrayNode('security')
376 1
            ->canBeEnabled()
377 1
            ->children();
378
379
        $validationRulesNode = $securityNode
380 1
            ->arrayNode('validation_rules')
381 1
            ->addDefaultsIfNotSet()
382 1
            ->children();
383
        $validationRulesNode
384 1
            ->integerNode('query_complexity')
385 1
            ->info('Query complexity score before execution. (Recommended >= 200)')
386 1
            ->min(0)
387 1
            ->defaultValue(QueryComplexity::DISABLED);
388
        $validationRulesNode
389 1
            ->integerNode('query_depth')
390 1
            ->info('Max depth of the query. (Recommended >= 11)')
391 1
            ->min(0)
392 1
            ->defaultValue(QueryDepth::DISABLED);
393
        $validationRulesNode
394 1
            ->booleanNode('disable_introspection')
395 1
            ->defaultFalse();
396 1
    }
397
398 1
    private function configureOthers(NodeBuilder $rootNode)
399
    {
400
        $rootNode
401 1
            ->scalarNode('id_encoder')
402 1
            ->defaultValue(SecureIDEncoder::class)
403 1
            ->info('Service used to encode nodes identifiers, must implements IDEncoderInterface');
404 1
    }
405
}
406