Passed
Push — master ( aa4c07...429b06 )
by Rafael
04:05
created

Configuration::configureCORS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 2
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 Symfony\Component\Config\Definition\Builder\NodeBuilder;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
/**
19
 * Class Configuration
20
 */
21
class Configuration implements ConfigurationInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getConfigTreeBuilder()
27
    {
28
        $treeBuilder = new TreeBuilder();
29
        /** @var NodeBuilder $rootNode */
30
        $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

30
        $rootNode = $treeBuilder->root('graphql')->/** @scrutinizer ignore-call */ addDefaultsIfNotSet()->children();
Loading history...
31
        $this->configureCORS($rootNode);
32
        $this->configureDefinition($rootNode);
33
34
        return $treeBuilder;
35
    }
36
37
    protected function configureCORS(NodeBuilder $root)
38
    {
39
        $cors = $root->arrayNode('cors')->canBeEnabled()->children();
40
        $cors->booleanNode('allow_credentials')->defaultTrue();
41
        $cors->variableNode('allow_headers')->defaultValue(['Origin', 'Content-Type', 'Accept', 'Authorization']);
42
        $cors->integerNode('max_age')->defaultValue(3600);
43
        $cors->variableNode('allow_methods')->defaultValue(['POST', 'GET', 'OPTIONS']);
44
        $cors->variableNode('allow_origins')->defaultValue(['*']);
45
    }
46
47
    protected function configureDefinition(NodeBuilder $root)
48
    {
49
        $definitions = $root->arrayNode('definitions')->addDefaultsIfNotSet()->children();
50
51
        $extensions = $definitions->arrayNode('extensions')->addDefaultsIfNotSet();
52
        $this->configureExtensionPagination($extensions->children());
53
        $this->configureExtensionNamespace($extensions->children());
54
    }
55
56
    protected function configureExtensionPagination(NodeBuilder $root)
57
    {
58
        $pagination = $root->arrayNode('pagination')->addDefaultsIfNotSet()->children();
59
        $pagination->integerNode('limit')
60
                   ->defaultValue(100)->info('Maximum limit allowed for all paginations');
61
    }
62
63
    protected function configureExtensionNamespace(NodeBuilder $root)
64
    {
65
        $namespaces = $root->arrayNode('namespaces')
66
                           ->info(
67
                               'Group GraphQL schema using namespaced schemas. 
68
On large schemas is  helpful to keep schemas grouped by bundle and node'
69
                           )
70
                           ->canBeEnabled()
71
                           ->addDefaultsIfNotSet()
72
                           ->children();
73
74
        $bundles = $namespaces->arrayNode('bundles')
75
                              ->info('Group each bundle into a separate schema definition')
76
                              ->canBeDisabled()
77
                              ->addDefaultsIfNotSet()
78
                              ->children();
79
80
        $bundles->scalarNode('suffix')
81
                ->info('The following suffix will be used for bundle groups')
82
                ->defaultValue('Bundle');
83
84
        $bundles->variableNode('ignore')
85
                ->info('The following bundles will be ignore for grouping, all definitions will be placed in the root query or mutation')
86
                ->defaultValue(['AppBundle']);
87
88
        $bundles->arrayNode('aliases')
89
                ->info(
90
                    'Define aliases for bundles to set definitions inside other desired bundle name. 
91
Can be used to group multiple bundles or publish a bundle with a different name'
92
                )
93
                ->example('SecurityBundle: AppBundle')
94
                ->useAttributeAsKey('name')
95
                ->prototype('scalar');
96
97
98
        $nodes = $namespaces->arrayNode('nodes')
99
                            ->info('Group queries and mutations of the same node into a node specific schema definition.')
100
                            ->addDefaultsIfNotSet()
101
                            ->canBeDisabled()
102
                            ->children();
103
104
        $nodes->scalarNode('query_suffix')
105
              ->info('The following suffix will be used to create the name for queries to the same node')
106
              ->defaultValue('Query');
107
108
        $nodes->scalarNode('mutation_suffix')
109
              ->info('The following suffix will be used to create the name for mutations to the same node')
110
              ->defaultValue('Mutation');
111
112
        $nodes->variableNode('ignore')
113
              ->info('The following nodes will be ignore for grouping, all definitions will be placed in the root query or mutation')
114
              ->defaultValue(['Node']);
115
116
        $nodes->arrayNode('aliases')
117
              ->info(
118
                  'Define aliases for nodes to set definitions inside other desired node name. 
119
Can be used to group multiple nodes or publish a node with a different group name'
120
              )
121
              ->example('InvoiceItem: Invoice')
122
              ->useAttributeAsKey('name')
123
              ->prototype('scalar');
124
    }
125
}
126