Issues (16)

src/DependencyInjection/Configuration.php (2 issues)

Labels
Severity
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
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
                ->addDefaultsIfNotSet()
77
                ->children()
78
                    ->booleanNode('enabled')
79
                        ->info(
80
                            'Set to true to enable the new request validation component.'.PHP_EOL.
81
                            'Set to false to disable request validation provided by this bundle.'
82
                        )
83
                        ->defaultTrue()
84
                        ->end()
85
                    ->booleanNode('parameter_validation')
0 ignored issues
show
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

85
                    ->/** @scrutinizer ignore-call */ booleanNode('parameter_validation')
Loading history...
86
                        ->info('Enables the experimental query parameter request validation.')
87
                        ->defaultFalse()
88
                        ->end()
89
                    ->end()
90
                ->end()
91
            ->end();
92
    }
93
94
    private function addExceptionsSection(ArrayNodeDefinition $rootNode): void
95
    {
96
        $rootNode->children()
97
            ->arrayNode('exception_handling')
98
                ->treatTrueLike(['enabled' => true])
99
                ->treatFalseLike(['enabled' => false])
100
                ->addDefaultsIfNotSet()
101
                ->children()
102
                    ->booleanNode('enabled')
103
                        ->info(
104
                            'Set to true to enable the new serialization-based exception handling.'.PHP_EOL.
105
                            'Set to false to disable exception handling provided by this bundle.'
106
                        )
107
                        ->defaultTrue()
108
                        ->end()
109
                    ->arrayNode('exceptions')
110
                        ->useAttributeAsKey('class')
111
                        ->arrayPrototype()
112
                            ->children()
113
                                ->scalarNode('class')
114
                                    ->info('The fully qualified class name of the exception.')
115
                                    ->cannotBeEmpty()
116
                                    ->end()
117
                                ->integerNode('status_code')
118
                                    ->info('The HTTP status code that must be sent when this exception occurs.')
119
                                    ->isRequired()
120
                                    ->min(100)
121
                                    ->max(999)
122
                                    ->end()
123
                                ->scalarNode('type_uri')
124
                                    ->info('The RFC 7807 URI reference that identifies the problem type. It will be sent with the response.')
125
                                    ->cannotBeEmpty()
126
                                    ->defaultValue('about:blank')
127
                                    ->end()
128
                                ->scalarNode('title')
129
                                    ->info('The RFC 7807 title that summarizes the problem type in human-readable language. It will be sent with the response.')
130
                                    ->cannotBeEmpty()
131
                                    ->defaultValue('An error occurred.')
132
                                    ->end()
133
                                ->booleanNode('add_instance_uri')
134
                                    ->defaultFalse()
135
                                    ->end()
136
                                ->end()
137
                            ->end()
138
                        ->end()
139
                    ->end()
140
                ->end()
141
            ->end();
142
    }
143
}
144