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

Configuration::addRoutingSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 13
rs 9.9
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
                    ->booleanNode('parameter_validation')
0 ignored issues
show
Bug introduced by
The method booleanNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

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