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

Configuration   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B addExceptionsSection() 0 62 1
A getConfigTreeBuilder() 0 8 1
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 Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
20
/**
21
 * Validates and merges configuration from the configuration files.
22
 *
23
 * @author Niels Nijens <[email protected]>
24
 */
25
class Configuration implements ConfigurationInterface
26
{
27
    public const BUNDLE_NAME = 'nijens/openapi-bundle';
28
29
    public function getConfigTreeBuilder(): TreeBuilder
30
    {
31
        $treeBuilder = new TreeBuilder('framework');
32
        $rootNode = $treeBuilder->getRootNode();
33
34
        $this->addExceptionsSection($rootNode);
35
36
        return $treeBuilder;
37
    }
38
39
    private function addExceptionsSection(ArrayNodeDefinition $rootNode): void
40
    {
41
        $rootNode->children()
42
            ->arrayNode('exception_handling')
43
                ->treatTrueLike(['enabled' => true])
44
                ->treatFalseLike(['enabled' => false])
45
                ->treatNullLike(['enabled' => null])
46
                ->addDefaultsIfNotSet()
47
                ->children()
48
                    ->booleanNode('enabled')
49
                        ->info(
50
                            'Set to true to enable the new serialization-based exception handling.'.PHP_EOL.
51
                            'Set to false to disable exception handling provided by this bundle.'.PHP_EOL.
52
                            'Set to null to keep using the deprecated exception JSON response builder.'
53
                        )
54
                        ->defaultNull()
55
                        ->validate()
56
                            ->ifNull()
57
                            ->then(function ($value) {
58
                                trigger_deprecation(
59
                                    self::BUNDLE_NAME,
60
                                    '1.3',
61
                                    'Setting the "nijens_openapi.exceptions.enabled" option to "null" is deprecated. It will default to "true" as of version 2.0.'
62
                                );
63
64
                                return $value;
65
                            })
66
                            ->end()
67
                        ->end()
68
                    ->arrayNode('exceptions')
1 ignored issue
show
Bug introduced by
The method arrayNode() 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

68
                    ->/** @scrutinizer ignore-call */ arrayNode('exceptions')
Loading history...
69
                        ->useAttributeAsKey('class')
70
                        ->arrayPrototype()
71
                            ->children()
72
                                ->scalarNode('class')
73
                                    ->info('The fully qualified class name of the exception.')
74
                                    ->cannotBeEmpty()
75
                                    ->end()
76
                                ->integerNode('status_code')
77
                                    ->info('The HTTP status code that must be sent when this exception occurs.')
78
                                    ->isRequired()
79
                                    ->min(100)
80
                                    ->max(999)
81
                                    ->end()
82
                                ->scalarNode('type_uri')
83
                                    ->info('The RFC 7807 URI reference that identifies the problem type. It will be sent with the response.')
84
                                    ->cannotBeEmpty()
85
                                    ->defaultValue('about:blank')
86
                                    ->end()
87
                                ->scalarNode('title')
88
                                    ->info('The RFC 7807 title that summarizes the problem type in human-readable language. It will be sent with the response.')
89
                                    ->cannotBeEmpty()
90
                                    ->defaultValue('An error occurred.')
91
                                    ->end()
92
                                ->booleanNode('add_instance_uri')
93
                                    ->defaultFalse()
94
                                    ->end()
95
                                ->end()
96
                            ->end()
97
                        ->end()
98
                    ->end()
99
                ->end()
100
            ->end();
101
    }
102
}
103