Completed
Push — main ( bdd72f...bbe282 )
by Niels
15s queued 12s
created

Configuration::addExceptionsSection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 56
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 62
rs 8.9599

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\DependencyInjection;
15
16
use Nijens\OpenapiBundle\ExceptionHandling\Exception\InvalidContentTypeProblemException;
17
use Nijens\OpenapiBundle\ExceptionHandling\Exception\InvalidRequestBodyProblemException;
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
20
use Symfony\Component\Config\Definition\ConfigurationInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Validates and merges configuration from the configuration files.
25
 *
26
 * @author Niels Nijens <[email protected]>
27
 */
28
class Configuration implements ConfigurationInterface
29
{
30
    public const BUNDLE_NAME = 'nijens/openapi-bundle';
31
32
    public const DEFAULT_EXCEPTION_HANDLING_EXCEPTIONS = [
33
        InvalidContentTypeProblemException::class => [
34
            'status_code' => Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
35
            'title' => 'The content type is not supported.',
36
        ],
37
        InvalidRequestBodyProblemException::class => [
38
            'status_code' => Response::HTTP_BAD_REQUEST,
39
            'title' => 'The request body contains errors.',
40
        ],
41
    ];
42
43
    public function getConfigTreeBuilder(): TreeBuilder
44
    {
45
        $treeBuilder = new TreeBuilder('nijens_openapi');
46
        $rootNode = $treeBuilder->getRootNode();
47
48
        $this->addExceptionsSection($rootNode);
49
50
        return $treeBuilder;
51
    }
52
53
    private function addExceptionsSection(ArrayNodeDefinition $rootNode): void
54
    {
55
        $rootNode->children()
56
            ->arrayNode('exception_handling')
57
                ->treatTrueLike(['enabled' => true])
58
                ->treatFalseLike(['enabled' => false])
59
                ->treatNullLike(['enabled' => null])
60
                ->addDefaultsIfNotSet()
61
                ->children()
62
                    ->booleanNode('enabled')
63
                        ->info(
64
                            'Set to true to enable the new serialization-based exception handling.'.PHP_EOL.
65
                            'Set to false to disable exception handling provided by this bundle.'.PHP_EOL.
66
                            'Set to null to keep using the deprecated exception JSON response builder.'
67
                        )
68
                        ->defaultNull()
69
                        ->validate()
70
                            ->ifNull()
71
                            ->then(function ($value) {
72
                                trigger_deprecation(
73
                                    self::BUNDLE_NAME,
74
                                    '1.3',
75
                                    'Setting the "nijens_openapi.exceptions.enabled" option to "null" is deprecated. It will default to "true" as of version 2.0.'
76
                                );
77
78
                                return $value;
79
                            })
80
                            ->end()
81
                        ->end()
82
                    ->arrayNode('exceptions')
83
                        ->useAttributeAsKey('class')
84
                        ->arrayPrototype()
85
                            ->children()
86
                                ->scalarNode('class')
87
                                    ->info('The fully qualified class name of the exception.')
88
                                    ->cannotBeEmpty()
89
                                    ->end()
90
                                ->integerNode('status_code')
91
                                    ->info('The HTTP status code that must be sent when this exception occurs.')
92
                                    ->isRequired()
93
                                    ->min(100)
94
                                    ->max(999)
95
                                    ->end()
96
                                ->scalarNode('type_uri')
97
                                    ->info('The RFC 7807 URI reference that identifies the problem type. It will be sent with the response.')
98
                                    ->cannotBeEmpty()
99
                                    ->defaultValue('about:blank')
100
                                    ->end()
101
                                ->scalarNode('title')
102
                                    ->info('The RFC 7807 title that summarizes the problem type in human-readable language. It will be sent with the response.')
103
                                    ->cannotBeEmpty()
104
                                    ->defaultValue('An error occurred.')
105
                                    ->end()
106
                                ->booleanNode('add_instance_uri')
107
                                    ->defaultFalse()
108
                                    ->end()
109
                                ->end()
110
                            ->end()
111
                        ->end()
112
                    ->end()
113
                ->end()
114
            ->end();
115
    }
116
}
117