Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

Configuration::configureDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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
20
/**
21
 * Class Configuration
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function getConfigTreeBuilder()
29
    {
30 1
        $treeBuilder = new TreeBuilder();
31
        /** @var NodeBuilder $rootNode */
32 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

32
        $rootNode = $treeBuilder->root('graphql')->/** @scrutinizer ignore-call */ addDefaultsIfNotSet()->children();
Loading history...
33 1
        $this->configureEndpoints($rootNode);
34 1
        $this->configureCORS($rootNode);
35 1
        $this->configureGraphiQL($rootNode);
36 1
        $this->configureDefinition($rootNode);
37 1
        $this->configureSecurity($rootNode);
38
39 1
        return $treeBuilder;
40
    }
41
42 1
    protected function configureEndpoints(NodeBuilder $root)
43
    {
44 1
        $endpoints = $root->arrayNode('endpoints')
45 1
                          ->useAttributeAsKey('name')
46 1
                          ->validate()
47 1
                          ->ifTrue(
48 1
                              function ($v) {
49 1
                                  return array_key_exists('default', $v);
50 1
                              }
51 1
                          )->thenInvalid('"default" can\'t be used as endpoint name, the system internally use this endpoint name to store the entire schema.')
52 1
                          ->end()
53 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

53
                          ->/** @scrutinizer ignore-call */ arrayPrototype()
Loading history...
54 1
                          ->children();
55
56 1
        $endpoints->arrayNode('roles')
57 1
                  ->beforeNormalization()
58 1
                  ->ifString()
59 1
                  ->then(
60 1
                      function ($v) {
61 1
                          return preg_split('/\s*,\s*/', $v);
62 1
                      }
63
                  )
64 1
                  ->end()
65 1
                  ->prototype('scalar')
66 1
                  ->end();
67
68 1
        $endpoints->scalarNode('host')->example('^api\.backend\.');
69 1
        $endpoints->scalarNode('path')->example('/backend');
70
71 1
        $root->arrayNode('endpoint_alias')
72 1
             ->info('Use alias to refer to multiple endpoints using only one name')
73 1
             ->useAttributeAsKey('name')
74 1
             ->beforeNormalization()
75 1
             ->ifString()
76 1
             ->then(
77 1
                 function ($v) {
78
                     return preg_split('/\s*,\s*/', $v);
79 1
                 }
80
             )
81 1
             ->end()
82 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

82
             ->/** @scrutinizer ignore-call */ variablePrototype();
Loading history...
83
84 1
        $root->scalarNode('endpoint_default')->info('Endpoint to apply to all definitions without explicit endpoint.');
85
86 1
    }
87
88 1
    protected function configureGraphiQL(NodeBuilder $root)
89
    {
90 1
        $graphiql = $root->arrayNode('graphiql')->addDefaultsIfNotSet()->children();
91
92 1
        $graphiql->scalarNode('title')
93 1
                 ->defaultValue('GraphQL API Explorer');
94
95
        $graphiql
96 1
            ->scalarNode('data_warning_message')
97 1
            ->defaultValue('Heads up! GraphQL Explorer makes use of your <strong>real</strong>, <strong>live</strong>, <strong>production</strong> data.');
98 1
        $graphiql->booleanNode('data_warning_dismissible')->defaultTrue();
99 1
        $graphiql->enumNode('data_warning_style')->values(['info', 'warning', 'danger'])->defaultValue('danger');
100
101 1
        $graphiql->scalarNode('template')
102 1
                 ->defaultValue('@YnloGraphQL/explorer.html.twig');
103
104 1
        $authentication = $graphiql->arrayNode('authentication')->addDefaultsIfNotSet()->children();
105
        $authentication
106 1
            ->booleanNode('required')
107 1
            ->info(
108 1
                'The API require credentials to make any requests, 
109
if this value is FALSE and a provider is specified the authentication is optional.'
110
            )
111 1
            ->defaultFalse();
112
113 1
        $authentication->scalarNode('login_message')
114 1
                       ->defaultValue('Start exploring GraphQL API queries using your account’s data now.');
115
116 1
        $authenticationProvider = $authentication->arrayNode('provider')->children();
117
118 1
        $jwt = $authenticationProvider->arrayNode('jwt')->canBeEnabled()->children();
119
120 1
        $jwtLogin = $jwt->arrayNode('login')->children();
121
122 1
        $jwtLogin->scalarNode('url')
123 1
                 ->info('Route name or URI to make the login process to retrieve the token.')
124 1
                 ->isRequired();
125
126 1
        $jwtLogin->scalarNode('username_parameter')
127 1
                 ->defaultValue('username');
128
129 1
        $jwtLogin->scalarNode('username_label')
130 1
                 ->defaultValue('Username');
131
132 1
        $jwtLogin->scalarNode('password_parameter')
133 1
                 ->defaultValue('password');
134
135 1
        $jwtLogin->scalarNode('password_label')
136 1
                 ->defaultValue('Password');
137
138 1
        $jwtLogin->enumNode('parameters_in')
139 1
                 ->values(['form', 'query', 'header'])
140 1
                 ->info('How pass parameters to request the token')
141 1
                 ->defaultValue('form');
142
143 1
        $jwtLogin->scalarNode('response_token_path')
144 1
                 ->defaultValue('token')
145 1
                 ->info('Where the token should be located in the response in case of JSON, set null if the response is the token.');
146
147 1
        $jwtRequests = $jwt->arrayNode('requests')->addDefaultsIfNotSet()->children();
148
149 1
        $jwtRequests->enumNode('token_in')
150 1
                    ->values(['query', 'header'])
151 1
                    ->info('Where should be located the token on every request')
152 1
                    ->defaultValue('header');
153
154 1
        $jwtRequests->scalarNode('token_name')
155 1
                    ->defaultValue('Authorization')
156 1
                    ->info('Name of the token in query or header name');
157
158 1
        $jwtRequests->scalarNode('token_template')
159 1
                    ->defaultValue('Bearer {token}')
160 1
                    ->info('Customize how the token should be send,  use the place holder {token} to replace for current token');
161
162 1
        $authenticationProvider->scalarNode('custom')
163 1
                               ->defaultNull()
164 1
                               ->info('Configure custom service to use as authentication provider');
165 1
    }
166
167 1
    protected function configureCORS(NodeBuilder $root)
168
    {
169 1
        $cors = $root->arrayNode('cors')->canBeEnabled()->children();
170 1
        $cors->booleanNode('allow_credentials')->defaultTrue();
171 1
        $cors->variableNode('allow_headers')->defaultValue(['Origin', 'Content-Type', 'Accept', 'Authorization']);
172 1
        $cors->integerNode('max_age')->defaultValue(3600);
173 1
        $cors->variableNode('allow_methods')->defaultValue(['POST', 'GET', 'OPTIONS']);
174 1
        $cors->variableNode('allow_origins')->defaultValue(['*']);
175 1
    }
176
177 1
    protected function configureDefinition(NodeBuilder $root)
178
    {
179 1
        $definitions = $root->arrayNode('definitions')->addDefaultsIfNotSet()->children();
180
181 1
        $plugins = $definitions->arrayNode('plugins')->addDefaultsIfNotSet();
182 1
        $this->configurePluginPaginationGlobalConfig($plugins->children());
183 1
        $this->configurePluginNamespaceGlobalConfig($plugins->children());
184 1
    }
185
186 1
    protected function configurePluginPaginationGlobalConfig(NodeBuilder $root)
187
    {
188 1
        $pagination = $root->arrayNode('pagination')->addDefaultsIfNotSet()->children();
189 1
        $pagination->integerNode('limit')
190 1
                   ->defaultValue(100)->info('Maximum limit allowed for all paginations');
191 1
    }
192
193 1
    protected function configurePluginNamespaceGlobalConfig(NodeBuilder $root)
194
    {
195 1
        $namespaces = $root->arrayNode('namespaces')
196 1
                           ->info(
197 1
                               'Group GraphQL schema using namespaced schemas. 
198
On large schemas is  helpful to keep schemas grouped by bundle and node'
199
                           )
200 1
                           ->canBeEnabled()
201 1
                           ->addDefaultsIfNotSet()
202 1
                           ->children();
203
204 1
        $bundles = $namespaces->arrayNode('bundles')
205 1
                              ->info('Group each bundle into a separate schema definition')
206 1
                              ->canBeDisabled()
207 1
                              ->addDefaultsIfNotSet()
208 1
                              ->children();
209
210 1
        $bundles->scalarNode('query_suffix')
211 1
                ->info('The following suffix will be used for bundle query groups')
212 1
                ->defaultValue('BundleQuery');
213
214 1
        $bundles->scalarNode('mutation_suffix')
215 1
                ->info('The following suffix will be used for bundle mutation groups')
216 1
                ->defaultValue('BundleMutation');
217
218 1
        $bundles->variableNode('ignore')
219 1
                ->info('The following bundles will be ignore for grouping, all definitions will be placed in the root query or mutation')
220 1
                ->defaultValue(['AppBundle']);
221
222 1
        $bundles->arrayNode('aliases')
223 1
                ->info(
224 1
                    'Define aliases for bundles to set definitions inside other desired bundle name. 
225
Can be used to group multiple bundles or publish a bundle with a different name'
226
                )
227 1
                ->example('SecurityBundle: AppBundle')
228 1
                ->useAttributeAsKey('name')
229 1
                ->prototype('scalar');
230
231
232 1
        $nodes = $namespaces->arrayNode('nodes')
233 1
                            ->info('Group queries and mutations of the same node into a node specific schema definition.')
234 1
                            ->addDefaultsIfNotSet()
235 1
                            ->canBeDisabled()
236 1
                            ->children();
237
238 1
        $nodes->scalarNode('query_suffix')
239 1
              ->info('The following suffix will be used to create the name for queries to the same node')
240 1
              ->defaultValue('Query');
241
242 1
        $nodes->scalarNode('mutation_suffix')
243 1
              ->info('The following suffix will be used to create the name for mutations to the same node')
244 1
              ->defaultValue('Mutation');
245
246 1
        $nodes->variableNode('ignore')
247 1
              ->info('The following nodes will be ignore for grouping, all definitions will be placed in the root query or mutation')
248 1
              ->defaultValue(['Node']);
249
250 1
        $nodes->arrayNode('aliases')
251 1
              ->info(
252 1
                  'Define aliases for nodes to set definitions inside other desired node name. 
253
Can be used to group multiple nodes or publish a node with a different group name'
254
              )
255 1
              ->example('InvoiceItem: Invoice')
256 1
              ->useAttributeAsKey('name')
257 1
              ->prototype('scalar');
258 1
    }
259
260 1
    private function configureSecurity(NodeBuilder $rootNode)
261
    {
262
        $securityNode = $rootNode
263 1
            ->arrayNode('security')
264 1
            ->canBeEnabled()
265 1
            ->children();
266
267
        $validationRulesNode = $securityNode
268 1
            ->arrayNode('validation_rules')
269 1
            ->addDefaultsIfNotSet()
270 1
            ->children();
271
        $validationRulesNode
272 1
            ->integerNode('query_complexity')
273 1
            ->info('Query complexity score before execution. (Recommended >= 200)')
274 1
            ->min(0)
275 1
            ->defaultValue(QueryComplexity::DISABLED);
276
        $validationRulesNode
277 1
            ->integerNode('query_depth')
278 1
            ->info('Max depth of the query. (Recommended >= 11)')
279 1
            ->min(0)
280 1
            ->defaultValue(QueryDepth::DISABLED);
281
        $validationRulesNode
282 1
            ->booleanNode('disable_introspection')
283 1
            ->defaultFalse();
284 1
    }
285
}
286