Test Failed
Pull Request — master (#8)
by David
04:55
created

Configuration::getOverridableHttpMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 1
rs 9.9332
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 21
    public function getConfigTreeBuilder(): TreeBuilder
19
    {
20 21
        $treeBuilder = new TreeBuilder('spiechu_symfony_commons');
21 21
        if (\method_exists($treeBuilder, 'getRootNode')) {
22 21
            $rootNode = $treeBuilder->getRootNode();
23
        } else {
24
            // BC layer for symfony/config 4.1 and older
25
            $rootNode = $treeBuilder->root('spiechu_symfony_commons');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root('spiechu_symfony_commons');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

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