Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
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
use Ynlo\GraphQLBundle\Encoder\SecureIDEncoder;
20
21
/**
22
 * Class Configuration
23
 */
24
class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function getConfigTreeBuilder()
30
    {
31 1
        $treeBuilder = new TreeBuilder();
32
        /** @var NodeBuilder $rootNode */
33 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

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

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

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