Completed
Push — main ( ce682b...8d0d4a )
by Niels
14s queued 12s
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->addExceptionsSection($rootNode);
50
51
        return $treeBuilder;
52
    }
53
54
    private function addRoutingSection(ArrayNodeDefinition $rootNode): void
55
    {
56
        $rootNode->children()
57
            ->arrayNode('routing')
58
                ->addDefaultsIfNotSet()
59
                ->children()
60
                    ->booleanNode('operation_id_as_route_name')
61
                        ->info('Toggle using the path item operation ID from the OpenAPI documents as route name.')
62
                        ->defaultFalse()
63
                        ->end()
64
                    ->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

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