Completed
Push — master ( 9d0a3d...651d7b )
by Dawid
02:56
created

Configuration::addApiVersionSupport()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 55
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 4.0047

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 42
cts 45
cp 0.9333
rs 9.078
c 0
b 0
f 0
cc 4
eloc 44
nc 1
nop 1
crap 4.0047

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
namespace Spiechu\SymfonyCommonsBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class Configuration implements ConfigurationInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     *
15
     * @throws \RuntimeException
16
     * @throws \InvalidArgumentException
17
     */
18 20
    public function getConfigTreeBuilder(): TreeBuilder
19
    {
20 20
        $treeBuilder = new TreeBuilder();
21 20
        $rootNode = $treeBuilder->root('spiechu_symfony_commons');
22
23 20
        $this->addGetMethodOverride($rootNode);
24 20
        $this->addResponseSchemaValidation($rootNode);
25 20
        $this->addApiVersionSupport($rootNode);
26
27 20
        return $treeBuilder;
28
    }
29
30
    /**
31
     * @param ArrayNodeDefinition $rootNode
32
     *
33
     * @throws \InvalidArgumentException
34
     * @throws \RuntimeException
35
     */
36 20
    protected function addGetMethodOverride(ArrayNodeDefinition $rootNode): void
37
    {
38 20
        $overridableHttpMethods = $this->getOverridableHttpMethods();
39
        $defaultOverridedHttpMethods = [
40 20
            Request::METHOD_DELETE,
41 20
            Request::METHOD_POST,
42 20
            Request::METHOD_PUT,
43
        ];
44
45
        $rootNode
46 20
            ->children()
47 20
                ->arrayNode('get_method_override')
48 20
                    ->info('Default options for GET method override feature')
49 20
                    ->addDefaultsIfNotSet()
50 20
                    ->canBeEnabled()
51 20
                    ->children()
52 20
                        ->scalarNode('listener_service_id')
53 20
                            ->cannotBeEmpty()
54 20
                            ->defaultValue('spiechu_symfony_commons.event_listener.get_method_override_listener')
55 20
                        ->end()
56 20
                        ->scalarNode('query_param_name')
57 20
                            ->cannotBeEmpty()
58 20
                            ->defaultValue('_method')
59 20
                            ->validate()
60
                                ->ifTrue(function ($methodName): bool {
61 1
                                    return !\is_string($methodName);
62 20
                                })
63 20
                                ->thenInvalid('Not a string provided')
64 20
                            ->end()
65 20
                        ->end()
66 20
                        ->arrayNode('allow_methods_override')
67 20
                            ->beforeNormalization()
68 20
                                ->ifString()->castToArray()
69 20
                            ->end()
70 20
                            ->defaultValue($defaultOverridedHttpMethods)
71 20
                            ->prototype('scalar')
72 20
                                ->validate()
73 20
                                    ->ifNotInArray($overridableHttpMethods)
74 20
                                    ->thenInvalid(sprintf(
75 20
                                        'Invalid methods to override provided, known are: "%s"',
76 20
                                        implode(', ', $overridableHttpMethods)
77
                                    ))
78 20
                                ->end()
79 20
                            ->end()
80 20
                            ->beforeNormalization()
81 20
                                ->ifArray()
82
                                ->then(function (array $methods): array {
83 3
                                    return array_unique(array_map('strtoupper', $methods));
84 20
                                })
85 20
                            ->end()
86 20
                        ->end()
87 20
                    ->end()
88 20
                ->end()
89 20
            ->end()
90 20
        ->end();
91 20
    }
92
93
    /**
94
     * @param ArrayNodeDefinition $rootNode
95
     */
96 20
    protected function addResponseSchemaValidation(ArrayNodeDefinition $rootNode): void
97
    {
98
        $rootNode
99 20
            ->children()
100 20
                ->arrayNode('response_schema_validation')
101 20
                    ->info('Default options for response schema validation feature')
102 20
                    ->addDefaultsIfNotSet()
103 20
                    ->canBeEnabled()
104 20
                    ->children()
105 20
                        ->booleanNode('throw_exception_when_format_not_found')->defaultTrue()->end()
106 20
                        ->scalarNode('failed_schema_check_listener_service_id')
107 20
                            ->defaultValue('spiechu_symfony_commons.event_listener.failed_schema_check_listener')
108 20
                        ->end()
109 20
                        ->booleanNode('disable_json_check_schema_subscriber')->defaultFalse()->end()
110 20
                        ->booleanNode('disable_xml_check_schema_subscriber')->defaultFalse()->end()
111 20
                    ->end()
112 20
                ->end()
113 20
            ->end()
114 20
        ->end();
115 20
    }
116
117
    /**
118
     * @param ArrayNodeDefinition $rootNode
119
     *
120
     * @throws \InvalidArgumentException
121
     * @throws \RuntimeException
122
     */
123 20
    protected function addApiVersionSupport(ArrayNodeDefinition $rootNode): void
124
    {
125
        $versionNormalizer = static function ($version): string {
126 4
            if (\is_string($version)) {
127
                return $version;
128
            }
129
130 4
            if (!is_numeric($version)) {
131
                throw new \InvalidArgumentException(sprintf('"%s" is not numeric', $version));
132
            }
133
134 4
            return number_format($version, 1, '.', '');
135 20
        };
136
137
        $rootNode
138 20
            ->children()
139 20
                ->arrayNode('api_versioning')
140 20
                    ->info('Default options for API versioning feature')
141 20
                    ->addDefaultsIfNotSet()
142 20
                    ->canBeEnabled()
143 20
                    ->children()
144 20
                        ->booleanNode('versioned_view_listener')->defaultFalse()->end()
145 20
                        ->arrayNode('features')
146 20
                            ->useAttributeAsKey('name')
147 20
                            ->prototype('array')
148 20
                                ->children()
149 20
                                    ->scalarNode('since')
150 20
                                        ->defaultNull()
151 20
                                        ->beforeNormalization()
152 20
                                            ->always($versionNormalizer)
153 20
                                        ->end()
154 20
                                    ->end()
155 20
                                    ->scalarNode('until')
156 20
                                        ->defaultNull()
157 20
                                        ->beforeNormalization()
158 20
                                            ->always($versionNormalizer)
159 20
                                        ->end()
160 20
                                    ->end()
161 20
                                ->end()
162 20
                                ->beforeNormalization()
163 20
                                    ->always(function ($prototypeValue): array {
164 4
                                        if (empty($prototypeValue)) {
165
                                            throw new \InvalidArgumentException('No version constraints provided');
166
                                        }
167
168 4
                                        return $prototypeValue;
169 20
                                    })
170 20
                                ->end()
171 20
                            ->end()
172 20
                        ->end()
173 20
                    ->end()
174 20
                ->end()
175 20
            ->end()
176 20
        ->end();
177 20
    }
178
179
    /**
180
     * @return string[]
181
     */
182 20
    protected function getOverridableHttpMethods(): array
183
    {
184
        return [
185 20
            Request::METHOD_CONNECT,
186 20
            Request::METHOD_DELETE,
187 20
            Request::METHOD_HEAD,
188 20
            Request::METHOD_OPTIONS,
189 20
            Request::METHOD_PATCH,
190 20
            Request::METHOD_POST,
191 20
            Request::METHOD_PURGE,
192 20
            Request::METHOD_TRACE,
193 20
            Request::METHOD_PUT,
194
        ];
195
    }
196
}
197