Passed
Push — master ( 724c6f...9da6fd )
by Rafael
06:22
created

Configuration::configureCORS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
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->booleanNode('show_trace')->info('Show error trace in debug mode')->defaultTrue();
55
56 1
        $errorHandling->scalarNode('formatter')
57 1
                      ->info('Formatter is responsible for converting instances of Error to an array')
58 1
                      ->defaultValue(DefaultErrorFormatter::class);
59
60 1
        $errorHandling->scalarNode('handler')
61 1
                      ->info('Handler is useful for error filtering and logging.')
62 1
                      ->defaultValue(DefaultErrorHandler::class);
63
64
        $controlledErrors = $errorHandling
65 1
            ->arrayNode('controlled_errors')
66 1
            ->info('Where to find for controlled errors')
67 1
            ->addDefaultsIfNotSet()
68 1
            ->children();
69
70
        $controlledErrors
71 1
            ->variableNode('locations')
72 1
            ->defaultValue(['Exception', 'Error'])
73 1
            ->info('Default folder to find exceptions and errors implementing controlled interface.')
74 1
            ->beforeNormalization()
75 1
            ->ifString()
76 1
            ->then(
77 1
                function ($v) {
78
                    return [$v];
79 1
                }
80
            )
81 1
            ->end();
82
83
        $controlledErrors
84 1
            ->variableNode('whitelist')
85 1
            ->info('White listed classes')
86 1
            ->defaultValue(['/App\\\\[Exception|Error]/', '/\w+Bundle\\\\[Exception|Error]/'])
87 1
            ->beforeNormalization()
88 1
            ->ifString()
89 1
            ->then(
90 1
                function ($v) {
91
                    return [$v];
92 1
                }
93
            )
94 1
            ->end()
95 1
            ->validate()
96 1
            ->ifTrue(
97 1
                function (array $value) {
98
                    foreach ($value as $val) {
99
                        try {
100
                            preg_match($val, null);
101
                        } catch (\Exception $exception) {
102
                            return true;
103
                        }
104
                    }
105 1
                }
106 1
            )->thenInvalid('Invalid regular expression');
107
108
        $controlledErrors
109 1
            ->variableNode('blacklist')
110 1
            ->info('Black listed classes')
111 1
            ->beforeNormalization()
112 1
            ->ifString()
113 1
            ->then(
114 1
                function ($v) {
115
                    return [$v];
116 1
                }
117
            )
118 1
            ->end()
119 1
            ->validate()
120 1
            ->ifTrue(
121 1
                function (array $value) {
122
                    foreach ($value as $val) {
123
                        try {
124
                            preg_match($val, null);
125
                        } catch (\Exception $exception) {
126
                            return true;
127
                        }
128
                    }
129 1
                }
130 1
            )->thenInvalid('Invalid regular expression');
131 1
    }
132
133 1
    protected function configureEndpoints(NodeBuilder $root)
134
    {
135 1
        $endpoints = $root->arrayNode('endpoints')
136 1
                          ->useAttributeAsKey('name')
137 1
                          ->validate()
138 1
                          ->ifTrue(
139 1
                              function ($v) {
140 1
                                  return array_key_exists('default', $v);
141 1
                              }
142 1
                          )->thenInvalid('"default" can\'t be used as endpoint name, the system internally use this endpoint name to store the entire schema.')
143 1
                          ->end()
144 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

144
                          ->/** @scrutinizer ignore-call */ arrayPrototype()
Loading history...
145 1
                          ->children();
146
147 1
        $endpoints->arrayNode('roles')
148 1
                  ->beforeNormalization()
149 1
                  ->ifString()
150 1
                  ->then(
151 1
                      function ($v) {
152 1
                          return preg_split('/\s*,\s*/', $v);
153 1
                      }
154
                  )
155 1
                  ->end()
156 1
                  ->prototype('scalar')
157 1
                  ->end();
158
159 1
        $endpoints->scalarNode('host')->example('^api\.backend\.');
160 1
        $endpoints->scalarNode('path')->example('/backend');
161
162 1
        $root->arrayNode('endpoint_alias')
163 1
             ->info('Use alias to refer to multiple endpoints using only one name')
164 1
             ->useAttributeAsKey('name')
165 1
             ->beforeNormalization()
166 1
             ->ifString()
167 1
             ->then(
168 1
                 function ($v) {
169
                     return preg_split('/\s*,\s*/', $v);
170 1
                 }
171
             )
172 1
             ->end()
173 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

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