Passed
Pull Request — main (#71)
by Niels
10:44
created

Configuration::addValidationSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 1
dl 0
loc 32
rs 9.504
c 0
b 0
f 0
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->addRoutingSection($rootNode);
49
        $this->addValidationSection($rootNode);
50
        $this->addExceptionsSection($rootNode);
51
52
        return $treeBuilder;
53
    }
54
55
    private function addRoutingSection(ArrayNodeDefinition $rootNode): void
56
    {
57
        $rootNode->children()
58
            ->arrayNode('routing')
59
                ->addDefaultsIfNotSet()
60
                ->children()
61
                    ->booleanNode('operation_id_as_route_name')
62
                        ->info('Toggle using the path item operation ID from the OpenAPI documents as route name.')
63
                        ->defaultFalse()
64
                        ->end()
65
                    ->end()
0 ignored issues
show
Bug introduced by
The method end() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Config...ion\Builder\TreeBuilder. Are you sure you never get one of those? ( Ignorable by Annotation )

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

65
                    ->/** @scrutinizer ignore-call */ end()
Loading history...
66
                ->end()
67
            ->end();
68
    }
69
70
    private function addValidationSection(ArrayNodeDefinition $rootNode): void
71
    {
72
        $rootNode->children()
73
            ->arrayNode('validation')
74
                ->treatTrueLike(['enabled' => true])
75
                ->treatFalseLike(['enabled' => false])
76
                ->treatNullLike(['enabled' => null])
77
                ->addDefaultsIfNotSet()
78
                ->children()
79
                    ->booleanNode('enabled')
80
                        ->info(
81
                            'Set to true to enable the new request validation component.'.PHP_EOL.
82
                            'Set to false to disable request validation provided by this bundle.'.PHP_EOL.
83
                            'Set to null to keep using the deprecated request validation.'
84
                        )
85
                        ->defaultNull()
86
                        ->validate()
87
                            ->ifNull()
88
                            ->then(function ($value) {
89
                                trigger_deprecation(
90
                                    self::BUNDLE_NAME,
91
                                    '1.5',
92
                                    'Setting the "nijens_openapi.validation.enabled" option to "null" is deprecated. It will default to "true" as of version 2.0.'
93
                                );
94
95
                                return $value;
96
                            })
97
                            ->end()
98
                        ->end()
99
                    ->end()
100
                ->end()
101
            ->end();
102
    }
103
104
    private function addExceptionsSection(ArrayNodeDefinition $rootNode): void
105
    {
106
        $rootNode->children()
107
            ->arrayNode('exception_handling')
108
                ->treatTrueLike(['enabled' => true])
109
                ->treatFalseLike(['enabled' => false])
110
                ->treatNullLike(['enabled' => null])
111
                ->addDefaultsIfNotSet()
112
                ->children()
113
                    ->booleanNode('enabled')
114
                        ->info(
115
                            'Set to true to enable the new serialization-based exception handling.'.PHP_EOL.
116
                            'Set to false to disable exception handling provided by this bundle.'.PHP_EOL.
117
                            'Set to null to keep using the deprecated exception JSON response builder.'
118
                        )
119
                        ->defaultNull()
120
                        ->validate()
121
                            ->ifNull()
122
                            ->then(function ($value) {
123
                                trigger_deprecation(
124
                                    self::BUNDLE_NAME,
125
                                    '1.3',
126
                                    'Setting the "nijens_openapi.exceptions.enabled" option to "null" is deprecated. It will default to "true" as of version 2.0.'
127
                                );
128
129
                                return $value;
130
                            })
131
                            ->end()
132
                        ->end()
133
                    ->arrayNode('exceptions')
134
                        ->useAttributeAsKey('class')
135
                        ->arrayPrototype()
136
                            ->children()
137
                                ->scalarNode('class')
138
                                    ->info('The fully qualified class name of the exception.')
139
                                    ->cannotBeEmpty()
140
                                    ->end()
141
                                ->integerNode('status_code')
142
                                    ->info('The HTTP status code that must be sent when this exception occurs.')
143
                                    ->isRequired()
144
                                    ->min(100)
145
                                    ->max(999)
146
                                    ->end()
147
                                ->scalarNode('type_uri')
148
                                    ->info('The RFC 7807 URI reference that identifies the problem type. It will be sent with the response.')
149
                                    ->cannotBeEmpty()
150
                                    ->defaultValue('about:blank')
151
                                    ->end()
152
                                ->scalarNode('title')
153
                                    ->info('The RFC 7807 title that summarizes the problem type in human-readable language. It will be sent with the response.')
154
                                    ->cannotBeEmpty()
155
                                    ->defaultValue('An error occurred.')
156
                                    ->end()
157
                                ->booleanNode('add_instance_uri')
158
                                    ->defaultFalse()
159
                                    ->end()
160
                                ->end()
161
                            ->end()
162
                        ->end()
163
                    ->end()
164
                ->end()
165
            ->end();
166
    }
167
}
168