Completed
Push — master ( 300eda...15c856 )
by Rafael
09:26
created

Configuration::configureGraphiQL()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 52
cts 52
cp 1
rs 8.9342
c 0
b 0
f 0
cc 1
eloc 54
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
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->configureCORS($rootNode);
34 1
        $this->configureGraphiQL($rootNode);
35 1
        $this->configureDefinition($rootNode);
36 1
        $this->configureSecurity($rootNode);
37
38 1
        return $treeBuilder;
39
    }
40
41 1
    protected function configureGraphiQL(NodeBuilder $root)
42
    {
43 1
        $graphiql = $root->arrayNode('graphiql')->addDefaultsIfNotSet()->children();
44
45 1
        $graphiql->scalarNode('title')
46 1
                 ->defaultValue('GraphQL API Explorer');
47
48
        $graphiql
49 1
            ->scalarNode('data_warning_message')
50 1
            ->defaultValue('Heads up! GraphQL Explorer makes use of your <strong>real</strong>, <strong>live</strong>, <strong>production</strong> data.');
51 1
        $graphiql->booleanNode('data_warning_dismissible')->defaultTrue();
52 1
        $graphiql->enumNode('data_warning_style')->values(['info', 'warning', 'danger'])->defaultValue('danger');
53
54 1
        $graphiql->scalarNode('template')
55 1
                 ->defaultValue('@YnloGraphQL/explorer.html.twig');
56
57 1
        $authentication = $graphiql->arrayNode('authentication')->addDefaultsIfNotSet()->children();
58
        $authentication
59 1
            ->booleanNode('required')
60 1
            ->info(
61 1
                'The API require credentials to make any requests, 
62
if this value is FALSE and a provider is specified the authentication is optional.'
63
            )
64 1
            ->defaultFalse();
65
66 1
        $authentication->scalarNode('login_message')
67 1
                       ->defaultValue('Start exploring GraphQL API queries using your account’s data now.');
68
69 1
        $authenticationProvider = $authentication->arrayNode('provider')->children();
70
71 1
        $jwt = $authenticationProvider->arrayNode('jwt')->canBeEnabled()->children();
72
73 1
        $jwtLogin = $jwt->arrayNode('login')->children();
74
75 1
        $jwtLogin->scalarNode('url')
76 1
                 ->info('Route name or URI to make the login process to retrieve the token.')
77 1
                 ->isRequired();
78
79 1
        $jwtLogin->scalarNode('username_parameter')
80 1
                 ->defaultValue('username');
81
82 1
        $jwtLogin->scalarNode('username_label')
83 1
                 ->defaultValue('Username');
84
85 1
        $jwtLogin->scalarNode('password_parameter')
86 1
                 ->defaultValue('password');
87 1
88 1
        $jwtLogin->scalarNode('password_label')
89
                 ->defaultValue('Password');
90 1
91 1
        $jwtLogin->enumNode('parameters_in')
92 1
                 ->values(['form', 'query', 'header'])
93
                 ->info('How pass parameters to request the token')
94 1
                 ->defaultValue('form');
95
96 1
        $jwtLogin->scalarNode('response_token_path')
97 1
                 ->defaultValue('token')
98 1
                 ->info('Where the token should be located in the response in case of JSON, set null if the response is the token.');
99 1
100
        $jwtRequests = $jwt->arrayNode('requests')->addDefaultsIfNotSet()->children();
101 1
102 1
        $jwtRequests->enumNode('token_in')
103 1
                    ->values(['query', 'header'])
104
                    ->info('Where should be located the token on every request')
105 1
                    ->defaultValue('header');
106 1
107 1
        $jwtRequests->scalarNode('token_name')
108
                    ->defaultValue('Authorization')
109 1
                    ->info('Name of the token in query or header name');
110 1
111 1
        $jwtRequests->scalarNode('token_template')
112 1
                    ->defaultValue('Bearer {token}')
113
                    ->info('Customize how the token should be send,  use the place holder {token} to replace for current token');
114 1
115
        $authenticationProvider->scalarNode('custom')
116 1
                               ->defaultNull()
117 1
                               ->info('Configure custom service to use as authentication provider');
118 1
    }
119 1
120 1
    protected function configureCORS(NodeBuilder $root)
121 1
    {
122 1
        $cors = $root->arrayNode('cors')->canBeEnabled()->children();
123
        $cors->booleanNode('allow_credentials')->defaultTrue();
124 1
        $cors->variableNode('allow_headers')->defaultValue(['Origin', 'Content-Type', 'Accept', 'Authorization']);
125
        $cors->integerNode('max_age')->defaultValue(3600);
126 1
        $cors->variableNode('allow_methods')->defaultValue(['POST', 'GET', 'OPTIONS']);
127
        $cors->variableNode('allow_origins')->defaultValue(['*']);
128 1
    }
129 1
130 1
    protected function configureDefinition(NodeBuilder $root)
131 1
    {
132
        $definitions = $root->arrayNode('definitions')->addDefaultsIfNotSet()->children();
133 1
134
        $extensions = $definitions->arrayNode('extensions')->addDefaultsIfNotSet();
135 1
        $this->configureExtensionPagination($extensions->children());
136 1
        $this->configureExtensionNamespace($extensions->children());
137 1
    }
138 1
139
    protected function configureExtensionPagination(NodeBuilder $root)
140 1
    {
141
        $pagination = $root->arrayNode('pagination')->addDefaultsIfNotSet()->children();
142 1
        $pagination->integerNode('limit')
143 1
                   ->defaultValue(100)->info('Maximum limit allowed for all paginations');
144 1
    }
145
146
    protected function configureExtensionNamespace(NodeBuilder $root)
147 1
    {
148 1
        $namespaces = $root->arrayNode('namespaces')
149 1
                           ->info(
150
                               'Group GraphQL schema using namespaced schemas. 
151 1
On large schemas is  helpful to keep schemas grouped by bundle and node'
152 1
                           )
153 1
                           ->canBeEnabled()
154 1
                           ->addDefaultsIfNotSet()
155 1
                           ->children();
156
157 1
        $bundles = $namespaces->arrayNode('bundles')
158 1
                              ->info('Group each bundle into a separate schema definition')
159 1
                              ->canBeDisabled()
160
                              ->addDefaultsIfNotSet()
161 1
                              ->children();
162 1
163 1
        $bundles->scalarNode('query_suffix')
164
                ->info('The following suffix will be used for bundle query groups')
165 1
                ->defaultValue('BundleQuery');
166 1
167 1
        $bundles->scalarNode('mutation_suffix')
168
                ->info('The following suffix will be used for bundle mutation groups')
169 1
                ->defaultValue('BundleMutation');
170 1
171 1
        $bundles->variableNode('ignore')
172
                ->info('The following bundles will be ignore for grouping, all definitions will be placed in the root query or mutation')
173
                ->defaultValue(['AppBundle']);
174 1
175 1
        $bundles->arrayNode('aliases')
176 1
                ->info(
177
                    'Define aliases for bundles to set definitions inside other desired bundle name. 
178
Can be used to group multiple bundles or publish a bundle with a different name'
179 1
                )
180 1
                ->example('SecurityBundle: AppBundle')
181 1
                ->useAttributeAsKey('name')
182 1
                ->prototype('scalar');
183 1
184
185 1
        $nodes = $namespaces->arrayNode('nodes')
186 1
                            ->info('Group queries and mutations of the same node into a node specific schema definition.')
187 1
                            ->addDefaultsIfNotSet()
188
                            ->canBeDisabled()
189 1
                            ->children();
190 1
191 1
        $nodes->scalarNode('query_suffix')
192
              ->info('The following suffix will be used to create the name for queries to the same node')
193 1
              ->defaultValue('Query');
194 1
195 1
        $nodes->scalarNode('mutation_suffix')
196
              ->info('The following suffix will be used to create the name for mutations to the same node')
197 1
              ->defaultValue('Mutation');
198 1
199 1
        $nodes->variableNode('ignore')
200
              ->info('The following nodes will be ignore for grouping, all definitions will be placed in the root query or mutation')
201
              ->defaultValue(['Node']);
202 1
203 1
        $nodes->arrayNode('aliases')
204 1
              ->info(
205 1
                  'Define aliases for nodes to set definitions inside other desired node name. 
206
Can be used to group multiple nodes or publish a node with a different group name'
207 1
              )
208
              ->example('InvoiceItem: Invoice')
209
              ->useAttributeAsKey('name')
210 1
              ->prototype('scalar');
211 1
    }
212 1
213
    private function configureSecurity(NodeBuilder $rootNode)
214
    {
215 1
        $securityNode = $rootNode
216 1
            ->arrayNode('security')
217 1
            ->canBeEnabled()
218
            ->children();
219 1
220 1
        $validationRulesNode = $securityNode
221 1
            ->arrayNode('validation_rules')
222 1
            ->addDefaultsIfNotSet()
223
            ->children();
224 1
        $validationRulesNode
225 1
            ->integerNode('query_complexity')
226 1
            ->info('Query complexity score before execution. (Recommended >= 200)')
227 1
            ->min(0)
228
            ->defaultValue(QueryComplexity::DISABLED);
229 1
        $validationRulesNode
230 1
            ->integerNode('query_depth')
231 1
            ->info('Max depth of the query. (Recommended >= 11)')
232
            ->min(0)
233
            ->defaultValue(QueryDepth::DISABLED);
234
        $validationRulesNode
235
            ->booleanNode('disable_introspection')
236
            ->defaultFalse();
237
    }
238
}
239