Completed
Push — 2.x ( e41763...7d61c5 )
by Christian
14s queued 11s
created

Configuration::testExceptionExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\Serializer\Encoder\XmlEncoder;
21
22
/**
23
 * This class contains the configuration information for the bundle.
24
 *
25
 * This information is solely responsible for how the different configuration
26
 * sections are normalized, and merged.
27
 *
28
 * @author Lukas Kahwe Smith <[email protected]>
29
 *
30
 * @internal
31
 */
32
final class Configuration implements ConfigurationInterface
33
{
34
    /**
35
     * Default debug mode value.
36
     *
37
     * @var bool
38
     */
39
    private $debug;
40
41 83
    public function __construct(bool $debug)
42
    {
43 83
        $this->debug = $debug;
44 83
    }
45
46 82
    public function getConfigTreeBuilder(): TreeBuilder
47
    {
48 82
        $treeBuilder = new TreeBuilder('fos_rest');
49
50 82
        if (method_exists($treeBuilder, 'getRootNode')) {
51 82
            $rootNode = $treeBuilder->getRootNode();
52
        } else {
53
            $rootNode = $treeBuilder->root('fos_rest');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

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

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

Loading history...
54
        }
55
56
        $rootNode
57 82
            ->children()
58 82
                ->scalarNode('disable_csrf_role')->defaultNull()->end()
59 82
                ->arrayNode('access_denied_listener')
60 82
                    ->canBeEnabled()
61 82
                    ->beforeNormalization()
62 View Code Duplication
                        ->ifArray()->then(function ($v) {
63
                            if (!empty($v) && empty($v['formats'])) {
64
                                unset($v['enabled']);
65
                                $v = ['enabled' => true, 'formats' => $v];
66
                            }
67
68
                            return $v;
69 82
                        })
70 82
                    ->end()
71 82
                    ->fixXmlConfig('format', 'formats')
72 82
                    ->children()
73 82
                        ->scalarNode('service')->defaultNull()->end()
74 82
                        ->arrayNode('formats')
75 82
                            ->useAttributeAsKey('name')
76 82
                            ->prototype('boolean')->end()
77 82
                        ->end()
78 82
                    ->end()
79 82
                ->end()
80 82
                ->scalarNode('unauthorized_challenge')->defaultNull()->end()
81 82
                ->arrayNode('param_fetcher_listener')
82 82
                    ->beforeNormalization()
83 82
                        ->ifString()
84 View Code Duplication
                        ->then(function ($v) {
85 1
                            return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
86 82
                        })
87 82
                    ->end()
88 82
                    ->canBeEnabled()
89 82
                    ->children()
90 82
                        ->booleanNode('force')->defaultFalse()->end()
91 82
                        ->scalarNode('service')->defaultNull()->end()
92 82
                    ->end()
93 82
                ->end()
94 82
                ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end()
95 82
                ->arrayNode('allowed_methods_listener')
96 82
                    ->canBeEnabled()
97 82
                    ->children()
98 82
                        ->scalarNode('service')->defaultNull()->end()
99 82
                    ->end()
100 82
                ->end()
101 82
                ->arrayNode('routing_loader')
102 82
                    ->addDefaultsIfNotSet()
103 82
                    ->beforeNormalization()
104
                        ->ifTrue(function ($v) { return isset($v['enabled']) && false !== $v['enabled']; })
105
                        ->then(function ($v) {
106
                            @trigger_error('Enabling the route generation feature is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
108
                            return $v;
109 82
                        })
110 82
                    ->end()
111 82
                    ->beforeNormalization()
112
                        ->ifTrue(function ($v) { return is_bool($v); })
113
                        ->then(function ($v) {
114
                            return [
115 65
                                'enabled' => $v,
116
                            ];
117 82
                        })
118 82
                    ->end()
119 82
                    ->children()
120 82
                        ->booleanNode('enabled')
121
                            ->defaultValue(function () {
122 6
                                @trigger_error('Enabling the route generation feature is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
123
124 6
                                return true;
125 82
                            })
126 82
                        ->end()
127 82
                        ->scalarNode('default_format')->defaultNull()->end()
128 82
                        ->scalarNode('prefix_methods')->defaultTrue()->end()
129 82
                        ->scalarNode('include_format')->defaultTrue()->end()
130 82
                    ->end()
131 82
                ->end()
132 82
                ->arrayNode('body_converter')
133 82
                    ->canBeEnabled()
134 82
                    ->children()
135 82
                        ->scalarNode('validate')
136 82
                            ->defaultFalse()
137 82
                            ->beforeNormalization()
138 82
                                ->ifTrue()
139
                                ->then(function ($value) {
140 3
                                    if (!class_exists(OptionsResolver::class)) {
141
                                        throw new InvalidConfigurationException("'body_converter.validate: true' requires OptionsResolver component installation ( composer require symfony/options-resolver )");
142
                                    }
143
144 3
                                    return $value;
145 82
                                })
146 82
                            ->end()
147 82
                        ->end()
148 82
                        ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end()
149 82
                    ->end()
150 82
                ->end()
151 82
                ->arrayNode('service')
152 82
                    ->addDefaultsIfNotSet()
153 82
                    ->children()
154 82
                        ->scalarNode('router')->defaultValue('router')->setDeprecated('The "%path%.%node%" configuration key has been deprecated in FOSRestBundle 2.8.')->end()
155 82
                        ->scalarNode('templating')
156 82
                            ->defaultValue('templating')
157 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
158 82
                        ->end()
159 82
                        ->scalarNode('serializer')->defaultNull()->end()
160 82
                        ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
161 82
                        ->scalarNode('inflector')
162 82
                            ->defaultValue('fos_rest.inflector.doctrine')
163 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
164 82
                        ->end()
165 82
                        ->scalarNode('validator')->defaultValue('validator')->end()
166 82
                    ->end()
167 82
                ->end()
168 82
                ->arrayNode('serializer')
169 82
                    ->addDefaultsIfNotSet()
170 82
                    ->children()
171 82
                        ->scalarNode('version')->defaultNull()->end()
172 82
                        ->arrayNode('groups')
173 82
                            ->prototype('scalar')->end()
174 82
                        ->end()
175 82
                        ->booleanNode('serialize_null')->defaultFalse()->end()
176 82
                    ->end()
177 82
                ->end()
178 82
                ->arrayNode('zone')
179 82
                    ->cannotBeOverwritten()
180 82
                    ->prototype('array')
181 82
                    ->fixXmlConfig('ip')
182 82
                    ->children()
183 82
                        ->scalarNode('path')
184 82
                            ->defaultNull()
185 82
                            ->info('use the urldecoded format')
186 82
                            ->example('^/path to resource/')
187 82
                        ->end()
188 82
                        ->scalarNode('host')->defaultNull()->end()
189 82
                        ->arrayNode('methods')
190
                            ->beforeNormalization()->ifString()->then(function ($v) {
191
                                return preg_split('/\s*,\s*/', $v);
192 82
                            })->end()
193 82
                            ->prototype('scalar')->end()
194 82
                        ->end()
195 82
                        ->arrayNode('ips')
196
                            ->beforeNormalization()->ifString()->then(function ($v) {
197 1
                                return array($v);
198 82
                            })->end()
199 82
                            ->prototype('scalar')->end()
200 82
                        ->end()
201 82
                    ->end()
202 82
                ->end()
203 82
            ->end()
204 82
        ->end();
205
206 82
        $this->addViewSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
207 82
        $this->addExceptionSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
208 82
        $this->addBodyListenerSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
209 82
        $this->addFormatListenerSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
210 82
        $this->addVersioningSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
211
212 82
        return $treeBuilder;
213
    }
214
215 82
    private function addViewSection(ArrayNodeDefinition $rootNode)
216
    {
217
        $rootNode
218 82
            ->children()
219 82
                ->arrayNode('view')
220 82
                    ->fixXmlConfig('format', 'formats')
221 82
                    ->fixXmlConfig('mime_type', 'mime_types')
222 82
                    ->fixXmlConfig('templating_format', 'templating_formats')
223 82
                    ->fixXmlConfig('force_redirect', 'force_redirects')
224 82
                    ->addDefaultsIfNotSet()
225 82
                    ->children()
226 82
                        ->scalarNode('default_engine')
227 82
                            ->setDeprecated('The "%path%.%node%" option has been deprecated in FOSRestBundle 2.8.')
228 82
                            ->defaultValue('twig')
229 82
                        ->end()
230 82
                        ->arrayNode('force_redirects')
231 82
                            ->setDeprecated('The "%path%.%node%" option has been deprecated in FOSRestBundle 2.8.')
232 82
                            ->useAttributeAsKey('name')
233 82
                            ->defaultValue(['html' => true])
234 82
                            ->prototype('boolean')->end()
235 82
                        ->end()
236 82
                        ->arrayNode('mime_types')
237 82
                            ->canBeEnabled()
238 82
                            ->beforeNormalization()
239 View Code Duplication
                                ->ifArray()->then(function ($v) {
240 1
                                    if (!empty($v) && empty($v['formats'])) {
241 1
                                        unset($v['enabled']);
242 1
                                        $v = ['enabled' => true, 'formats' => $v];
243
                                    }
244
245 1
                                    return $v;
246 82
                                })
247 82
                            ->end()
248 82
                            ->fixXmlConfig('format', 'formats')
249 82
                            ->children()
250 82
                                ->scalarNode('service')->defaultNull()->end()
251 82
                                ->arrayNode('formats')
252 82
                                    ->useAttributeAsKey('name')
253 82
                                    ->prototype('array')
254 82
                                        ->beforeNormalization()
255 82
                                            ->ifString()
256
                                            ->then(function ($v) { return array($v); })
257 82
                                        ->end()
258 82
                                        ->prototype('scalar')->end()
259 82
                                    ->end()
260 82
                                ->end()
261 82
                            ->end()
262 82
                        ->end()
263 82
                        ->arrayNode('formats')
264 82
                            ->useAttributeAsKey('name')
265 82
                            ->defaultValue(['json' => true, 'xml' => true])
266 82
                            ->prototype('boolean')->end()
267 82
                        ->end()
268 82
                        ->arrayNode('templating_formats')
269 82
                            ->setDeprecated('The "%path%.%node%" configuration key has been deprecated in FOSRestBundle 2.8.')
270 82
                            ->useAttributeAsKey('name')
271 82
                            ->defaultValue(['html' => true])
272 82
                            ->prototype('boolean')->end()
273 82
                        ->end()
274 82
                        ->arrayNode('view_response_listener')
275 82
                            ->beforeNormalization()
276 82
                                ->ifString()
277 View Code Duplication
                                ->then(function ($v) {
278 9
                                    return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
279 82
                                })
280 82
                            ->end()
281 82
                            ->canBeEnabled()
282 82
                            ->children()
283 82
                                ->booleanNode('force')->defaultFalse()->end()
284 82
                                ->scalarNode('service')->defaultNull()->end()
285 82
                            ->end()
286 82
                        ->end()
287 82
                        ->scalarNode('failed_validation')->defaultValue(Response::HTTP_BAD_REQUEST)->end()
288 82
                        ->scalarNode('empty_content')->defaultValue(Response::HTTP_NO_CONTENT)->end()
289 82
                        ->booleanNode('serialize_null')->defaultFalse()->end()
290 82
                        ->arrayNode('jsonp_handler')
291 82
                            ->canBeUnset()
292 82
                            ->children()
293 82
                                ->scalarNode('callback_param')->defaultValue('callback')->end()
294 82
                                ->scalarNode('mime_type')->defaultValue('application/javascript+jsonp')->end()
295 82
                            ->end()
296 82
                        ->end()
297 82
                    ->end()
298 82
                ->end()
299 82
            ->end();
300 82
    }
301
302 82
    private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
303
    {
304 82
        $decodersDefaultValue = ['json' => 'fos_rest.decoder.json'];
305 82
        if (class_exists(XmlEncoder::class)) {
306 82
            $decodersDefaultValue['xml'] = 'fos_rest.decoder.xml';
307
        }
308
        $rootNode
309 82
            ->children()
310 82
                ->arrayNode('body_listener')
311 82
                    ->fixXmlConfig('decoder', 'decoders')
312 82
                    ->addDefaultsIfNotSet()
313 82
                    ->canBeUnset()
314 82
                    ->treatFalseLike(['enabled' => false])
315 82
                    ->treatTrueLike(['enabled' => true])
316 82
                    ->treatNullLike(['enabled' => true])
317 82
                    ->children()
318 82
                        ->booleanNode('enabled')
319
                            ->defaultValue(function () {
320 6
                                @trigger_error('The body_listener config has been enabled by default and will be disabled by default in FOSRestBundle 3.0. Please enable or disable it explicitly.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
321
322 6
                                return true;
323 82
                            })
324 82
                        ->end()
325 82
                        ->scalarNode('service')->defaultNull()->end()
326 82
                        ->scalarNode('default_format')->defaultNull()->end()
327 82
                        ->booleanNode('throw_exception_on_unsupported_content_type')
328 82
                            ->defaultFalse()
329 82
                        ->end()
330 82
                        ->arrayNode('decoders')
331 82
                            ->useAttributeAsKey('name')
332 82
                            ->defaultValue($decodersDefaultValue)
333 82
                            ->prototype('scalar')->end()
334 82
                        ->end()
335 82
                        ->arrayNode('array_normalizer')
336 82
                            ->addDefaultsIfNotSet()
337 82
                            ->beforeNormalization()
338
                                ->ifString()->then(function ($v) {
339 1
                                    return ['service' => $v];
340 82
                                })
341 82
                            ->end()
342 82
                            ->children()
343 82
                                ->scalarNode('service')->defaultNull()->end()
344 82
                                ->booleanNode('forms')->defaultFalse()->end()
345 82
                            ->end()
346 82
                        ->end()
347 82
                    ->end()
348 82
                ->end()
349 82
            ->end();
350 82
    }
351
352 82
    private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
353
    {
354
        $rootNode
355 82
            ->children()
356 82
                ->arrayNode('format_listener')
357 82
                    ->fixXmlConfig('rule', 'rules')
358 82
                    ->addDefaultsIfNotSet()
359 82
                    ->canBeUnset()
360 82
                    ->beforeNormalization()
361
                        ->ifTrue(function ($v) {
362
                            // check if we got an assoc array in rules
363 9
                            return isset($v['rules'])
364 9
                                && is_array($v['rules'])
365 9
                                && array_keys($v['rules']) !== range(0, count($v['rules']) - 1);
366 82
                        })
367
                        ->then(function ($v) {
368 1
                            $v['rules'] = [$v['rules']];
369
370 1
                            return $v;
371 82
                        })
372 82
                    ->end()
373 82
                    ->canBeEnabled()
374 82
                    ->children()
375 82
                        ->scalarNode('service')->defaultNull()->end()
376 82
                        ->arrayNode('rules')
377 82
                            ->performNoDeepMerging()
378 82
                            ->prototype('array')
379 82
                                ->fixXmlConfig('priority', 'priorities')
380 82
                                ->fixXmlConfig('attribute', 'attributes')
381 82
                                ->children()
382 82
                                    ->scalarNode('path')->defaultNull()->info('URL path info')->end()
383 82
                                    ->scalarNode('host')->defaultNull()->info('URL host name')->end()
384 82
                                    ->variableNode('methods')->defaultNull()->info('Method for URL')->end()
385 82
                                    ->arrayNode('attributes')
386 82
                                        ->useAttributeAsKey('name')
387 82
                                        ->prototype('variable')->end()
388 82
                                    ->end()
389 82
                                    ->booleanNode('stop')->defaultFalse()->end()
390 82
                                    ->booleanNode('prefer_extension')->defaultTrue()->end()
391 82
                                    ->scalarNode('fallback_format')->defaultValue('html')->end()
392 82
                                    ->arrayNode('priorities')
393
                                        ->beforeNormalization()->ifString()->then(function ($v) {
394
                                            return preg_split('/\s*,\s*/', $v);
395 82
                                        })->end()
396 82
                                        ->prototype('scalar')->end()
397 82
                                    ->end()
398 82
                                ->end()
399 82
                            ->end()
400 82
                        ->end()
401 82
                    ->end()
402 82
                ->end()
403 82
            ->end();
404 82
    }
405
406 82
    private function addVersioningSection(ArrayNodeDefinition $rootNode)
407
    {
408
        $rootNode
409 82
        ->children()
410 82
            ->arrayNode('versioning')
411 82
                ->canBeEnabled()
412 82
                ->children()
413 82
                    ->scalarNode('default_version')->defaultNull()->end()
414 82
                    ->arrayNode('resolvers')
415 82
                        ->addDefaultsIfNotSet()
416 82
                        ->children()
417 82
                            ->arrayNode('query')
418 82
                                ->canBeDisabled()
419 82
                                ->children()
420 82
                                    ->scalarNode('parameter_name')->defaultValue('version')->end()
421 82
                                ->end()
422 82
                            ->end()
423 82
                            ->arrayNode('custom_header')
424 82
                                ->canBeDisabled()
425 82
                                ->children()
426 82
                                    ->scalarNode('header_name')->defaultValue('X-Accept-Version')->end()
427 82
                                ->end()
428 82
                            ->end()
429 82
                            ->arrayNode('media_type')
430 82
                                ->canBeDisabled()
431 82
                                ->children()
432 82
                                    ->scalarNode('regex')->defaultValue('/(v|version)=(?P<version>[0-9\.]+)/')->end()
433 82
                                ->end()
434 82
                            ->end()
435 82
                        ->end()
436 82
                    ->end()
437 82
                    ->arrayNode('guessing_order')
438 82
                        ->defaultValue(['query', 'custom_header', 'media_type'])
439 82
                        ->validate()
440
                            ->ifTrue(function ($v) {
441
                                foreach ($v as $resolver) {
442
                                    if (!in_array($resolver, ['query', 'custom_header', 'media_type'])) {
443
                                        return true;
444
                                    }
445
                                }
446 82
                            })
447 82
                            ->thenInvalid('Versioning guessing order can only contain "query", "custom_header", "media_type".')
448 82
                        ->end()
449 82
                        ->prototype('scalar')->end()
450 82
                    ->end()
451 82
                ->end()
452 82
            ->end()
453 82
        ->end();
454 82
    }
455
456 82
    private function addExceptionSection(ArrayNodeDefinition $rootNode)
457
    {
458
        $rootNode
459 82
            ->children()
460 82
                ->arrayNode('exception')
461 82
                    ->fixXmlConfig('code', 'codes')
462 82
                    ->fixXmlConfig('message', 'messages')
463 82
                    ->addDefaultsIfNotSet()
464 82
                    ->canBeEnabled()
465 82
                    ->children()
466 82
                        ->booleanNode('map_exception_codes')
467 82
                            ->defaultFalse()
468 82
                            ->info('Enables an event listener that maps exception codes to response status codes based on the map configured with the "fos_rest.exception.codes" option.')
469 82
                        ->end()
470 82
                        ->booleanNode('exception_listener')
471
                            ->defaultValue(function () {
472 14
                                @trigger_error('Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
473
474 14
                                return true;
475 82
                            })
476 82
                            ->beforeNormalization()
477 82
                                ->ifTrue()
478
                                ->then(function ($v) {
479
                                    @trigger_error('Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
480
481
                                    return $v;
482 82
                                })
483 82
                            ->end()
484 82
                        ->end()
485 82
                        ->scalarNode('exception_controller')
486 82
                            ->defaultNull()
487 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
488 82
                        ->end()
489 82
                        ->booleanNode('serialize_exceptions')
490
                            ->defaultValue(function () {
491 9
                                @trigger_error('Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
492
493 9
                                return true;
494 82
                            })
495 82
                            ->beforeNormalization()
496 82
                                ->ifTrue()
497
                                ->then(function ($v) {
498 8
                                    @trigger_error('Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
499
500 8
                                    return $v;
501 82
                                })
502 82
                            ->end()
503 82
                        ->end()
504 82
                        ->enumNode('flatten_exception_format')
505 82
                            ->defaultValue('legacy')
506 82
                            ->values(['legacy', 'rfc7807'])
507 82
                        ->end()
508 82
                        ->scalarNode('service')
509 82
                            ->defaultNull()
510 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
511 82
                        ->end()
512 82
                        ->booleanNode('serializer_error_renderer')->defaultValue(false)->end()
513 82
                        ->arrayNode('codes')
514 82
                            ->useAttributeAsKey('name')
515 82
                            ->beforeNormalization()
516 82
                                ->ifArray()
517
                                ->then(function (array $items) {
518 13
                                    foreach ($items as &$item) {
519 13
                                        if (is_int($item)) {
520 3
                                            continue;
521
                                        }
522
523 10
                                        if (!defined(sprintf('%s::%s', Response::class, $item))) {
524 9
                                            throw new InvalidConfigurationException(sprintf('Invalid HTTP code in fos_rest.exception.codes, see %s for all valid codes.', Response::class));
525
                                        }
526
527 1
                                        $item = constant(sprintf('%s::%s', Response::class, $item));
528
                                    }
529
530 4
                                    return $items;
531 82
                                })
532 82
                            ->end()
533 82
                            ->prototype('integer')->end()
534
535 82
                            ->validate()
536 82
                            ->ifArray()
537
                                ->then(function (array $items) {
538 4
                                    foreach ($items as $class => $code) {
539 4
                                        $this->testExceptionExists($class);
540
                                    }
541
542 3
                                    return $items;
543 82
                                })
544 82
                            ->end()
545 82
                        ->end()
546 82
                        ->arrayNode('messages')
547 82
                            ->useAttributeAsKey('name')
548 82
                            ->prototype('boolean')->end()
549 82
                            ->validate()
550 82
                                ->ifArray()
551
                                ->then(function (array $items) {
552 12
                                    foreach ($items as $class => $nomatter) {
553 12
                                        $this->testExceptionExists($class);
554
                                    }
555
556 11
                                    return $items;
557 82
                                })
558 82
                            ->end()
559 82
                        ->end()
560 82
                        ->booleanNode('debug')
561 82
                            ->defaultValue($this->debug)
562 82
                        ->end()
563 82
                    ->end()
564 82
                ->end()
565 82
            ->end();
566 82
    }
567
568 16
    private function testExceptionExists(string $throwable)
569
    {
570 16
        if (!is_subclass_of($throwable, \Throwable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Throwable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
571 2
            throw new InvalidConfigurationException(sprintf('FOSRestBundle exception mapper: Could not load class "%s" or the class does not extend from "%s". Most probably this is a configuration problem.', $throwable, \Throwable::class));
572
        }
573 14
    }
574
}
575