Passed
Push — master ( 36097f...93304f )
by Rafael
03:14
created

Configuration::configureGraphiQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 48
cts 48
cp 1
rs 9.1369
c 0
b 0
f 0
cc 1
eloc 50
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('password_parameter')
83 1
                 ->defaultValue('password');
84
85 1
        $jwtLogin->enumNode('parameters_in')
86 1
                 ->values(['form', 'query', 'header'])
87 1
                 ->info('How pass parameters to request the token')
88 1
                 ->defaultValue('form');
89
90 1
        $jwtLogin->scalarNode('response_token_path')
91 1
                 ->defaultValue('token')
92 1
                 ->info('Where the token should be located in the response in case of JSON, set null if the response is the token.');
93
94 1
        $jwtRequests = $jwt->arrayNode('requests')->addDefaultsIfNotSet()->children();
95
96 1
        $jwtRequests->enumNode('token_in')
97 1
                    ->values(['query', 'header'])
98 1
                    ->info('Where should be located the token on every request')
99 1
                    ->defaultValue('header');
100
101 1
        $jwtRequests->scalarNode('token_name')
102 1
                    ->defaultValue('Authorization')
103 1
                    ->info('Name of the token in query or header name');
104
105 1
        $jwtRequests->scalarNode('token_template')
106 1
                    ->defaultValue('Bearer {token}')
107 1
                    ->info('Customize how the token should be send,  use the place holder {token} to replace for current token');
108
109 1
        $authenticationProvider->scalarNode('custom')
110 1
                               ->defaultNull()
111 1
                               ->info('Configure custom service to use as authentication provider');
112 1
    }
113
114 1
    protected function configureCORS(NodeBuilder $root)
115
    {
116 1
        $cors = $root->arrayNode('cors')->canBeEnabled()->children();
117 1
        $cors->booleanNode('allow_credentials')->defaultTrue();
118 1
        $cors->variableNode('allow_headers')->defaultValue(['Origin', 'Content-Type', 'Accept', 'Authorization']);
119 1
        $cors->integerNode('max_age')->defaultValue(3600);
120 1
        $cors->variableNode('allow_methods')->defaultValue(['POST', 'GET', 'OPTIONS']);
121 1
        $cors->variableNode('allow_origins')->defaultValue(['*']);
122 1
    }
123
124 1
    protected function configureDefinition(NodeBuilder $root)
125
    {
126 1
        $definitions = $root->arrayNode('definitions')->addDefaultsIfNotSet()->children();
127
128 1
        $extensions = $definitions->arrayNode('extensions')->addDefaultsIfNotSet();
129 1
        $this->configureExtensionPagination($extensions->children());
130 1
        $this->configureExtensionNamespace($extensions->children());
131 1
    }
132
133 1
    protected function configureExtensionPagination(NodeBuilder $root)
134
    {
135 1
        $pagination = $root->arrayNode('pagination')->addDefaultsIfNotSet()->children();
136 1
        $pagination->integerNode('limit')
137 1
                   ->defaultValue(100)->info('Maximum limit allowed for all paginations');
138 1
    }
139
140 1
    protected function configureExtensionNamespace(NodeBuilder $root)
141
    {
142 1
        $namespaces = $root->arrayNode('namespaces')
143 1
                           ->info(
144 1
                               'Group GraphQL schema using namespaced schemas. 
145
On large schemas is  helpful to keep schemas grouped by bundle and node'
146
                           )
147 1
                           ->canBeEnabled()
148 1
                           ->addDefaultsIfNotSet()
149 1
                           ->children();
150
151 1
        $bundles = $namespaces->arrayNode('bundles')
152 1
                              ->info('Group each bundle into a separate schema definition')
153 1
                              ->canBeDisabled()
154 1
                              ->addDefaultsIfNotSet()
155 1
                              ->children();
156
157 1
        $bundles->scalarNode('query_suffix')
158 1
                ->info('The following suffix will be used for bundle query groups')
159 1
                ->defaultValue('BundleQuery');
160
161 1
        $bundles->scalarNode('mutation_suffix')
162 1
                ->info('The following suffix will be used for bundle mutation groups')
163 1
                ->defaultValue('BundleMutation');
164
165 1
        $bundles->variableNode('ignore')
166 1
                ->info('The following bundles will be ignore for grouping, all definitions will be placed in the root query or mutation')
167 1
                ->defaultValue(['AppBundle']);
168
169 1
        $bundles->arrayNode('aliases')
170 1
                ->info(
171 1
                    'Define aliases for bundles to set definitions inside other desired bundle name. 
172
Can be used to group multiple bundles or publish a bundle with a different name'
173
                )
174 1
                ->example('SecurityBundle: AppBundle')
175 1
                ->useAttributeAsKey('name')
176 1
                ->prototype('scalar');
177
178
179 1
        $nodes = $namespaces->arrayNode('nodes')
180 1
                            ->info('Group queries and mutations of the same node into a node specific schema definition.')
181 1
                            ->addDefaultsIfNotSet()
182 1
                            ->canBeDisabled()
183 1
                            ->children();
184
185 1
        $nodes->scalarNode('query_suffix')
186 1
              ->info('The following suffix will be used to create the name for queries to the same node')
187 1
              ->defaultValue('Query');
188
189 1
        $nodes->scalarNode('mutation_suffix')
190 1
              ->info('The following suffix will be used to create the name for mutations to the same node')
191 1
              ->defaultValue('Mutation');
192
193 1
        $nodes->variableNode('ignore')
194 1
              ->info('The following nodes will be ignore for grouping, all definitions will be placed in the root query or mutation')
195 1
              ->defaultValue(['Node']);
196
197 1
        $nodes->arrayNode('aliases')
198 1
              ->info(
199 1
                  'Define aliases for nodes to set definitions inside other desired node name. 
200
Can be used to group multiple nodes or publish a node with a different group name'
201
              )
202 1
              ->example('InvoiceItem: Invoice')
203 1
              ->useAttributeAsKey('name')
204 1
              ->prototype('scalar');
205 1
    }
206
207 1
    private function configureSecurity(NodeBuilder $rootNode)
208
    {
209
        $securityNode = $rootNode
210 1
            ->arrayNode('security')
211 1
            ->canBeEnabled()
212 1
            ->children();
213
214
        $validationRulesNode = $securityNode
215 1
            ->arrayNode('validation_rules')
216 1
            ->addDefaultsIfNotSet()
217 1
            ->children();
218
        $validationRulesNode
219 1
            ->integerNode('query_complexity')
220 1
            ->info('Query complexity score before execution. (Recommended >= 200)')
221 1
            ->min(0)
222 1
            ->defaultValue(QueryComplexity::DISABLED);
223
        $validationRulesNode
224 1
            ->integerNode('query_depth')
225 1
            ->info('Max depth of the query. (Recommended >= 11)')
226 1
            ->min(0)
227 1
            ->defaultValue(QueryDepth::DISABLED);
228
        $validationRulesNode
229 1
            ->booleanNode('disable_introspection')
230 1
            ->defaultFalse();
231 1
    }
232
}
233