Completed
Pull Request — 2.0 (#996)
by
unknown
02:54
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 95
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 8.4117
c 0
b 0
f 0
cc 1
eloc 84
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection;
13
14
use ApiPlatform\Core\Exception\InvalidArgumentException;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Serializer\Exception\ExceptionInterface;
21
22
/**
23
 * The configuration of the bundle.
24
 *
25
 * @author Kévin Dunglas <[email protected]>
26
 * @author Baptiste Meyer <[email protected]>
27
 */
28
final class Configuration implements ConfigurationInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getConfigTreeBuilder()
34
    {
35
        $treeBuilder = new TreeBuilder();
36
        $rootNode = $treeBuilder->root('api_platform');
37
38
        $rootNode
39
            ->children()
40
                ->scalarNode('title')->defaultValue('')->info('The title of the API.')->end()
41
                ->scalarNode('description')->defaultValue('')->info('The description of the API.')->end()
42
                ->scalarNode('version')->defaultValue('0.0.0')->info('The version of the API.')->end()
43
                ->scalarNode('default_operation_path_resolver')->defaultValue('api_platform.operation_path_resolver.underscore')->info('Specify the default operation path resolver to use for generating resources operations path.')->end()
44
                ->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
45
                ->arrayNode('eager_loading')
46
                    ->canBeDisabled()
47
                    ->addDefaultsIfNotSet()
48
                    ->children()
49
                        ->booleanNode('enabled')->defaultTrue()->info('To enable or disable eager loading')->end()
50
                        ->integerNode('max_joins')->defaultValue(30)->info('Max number of joined relations before EagerLoading throws a RuntimeException')->end()
51
                        ->booleanNode('force_eager')->defaultTrue()->info('Force join on every relation. If disabled, it will only join relations having the EAGER fetch mode.')->end()
52
                    ->end()
53
                ->end()
54
                ->booleanNode('enable_fos_user')->defaultValue(false)->info('Enable the FOSUserBundle integration.')->end()
55
                ->booleanNode('enable_nelmio_api_doc')->defaultValue(false)->info('Enable the Nelmio Api doc integration.')->end()
56
                ->booleanNode('enable_swagger')->defaultValue(true)->info('Enable the Swagger documentation and export.')->end()
57
                ->booleanNode('enable_swagger_ui')->defaultValue(true)->info('Enable Swagger ui.')->end()
58
59
                ->arrayNode('oauth')
60
                    ->canBeEnabled()
61
                    ->addDefaultsIfNotSet()
62
                    ->children()
63
                        ->booleanNode('enabled')->defaultFalse()->info('To enable or disable oauth')->end()
64
                        ->scalarNode('clientId')->defaultValue('')->info('The oauth client id.')->end()
65
                        ->scalarNode('clientSecret')->defaultValue('')->info('The oauth client secret.')->end()
66
                        ->scalarNode('type')->defaultValue('oauth2')->info('The oauth client secret.')->end()
67
                        ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
68
                        ->scalarNode('tokenUrl')->defaultValue('/oauth/v2/token')->info('The oauth token url.')->end()
69
                        ->scalarNode('authorizationUrl')->defaultValue('/oauth/v2/auth')->info('The oauth authentication url.')->end()
70
                        ->arrayNode('scopes')
71
                            ->prototype('scalar')->end()
72
                        ->end()
73
                    ->end()
74
                ->end()
75
76
                ->arrayNode('collection')
77
                    ->addDefaultsIfNotSet()
78
                    ->children()
79
                        ->scalarNode('order')->defaultValue('ASC')->info('The default order of results.')->end() // Default ORDER is required for postgresql and mysql >= 5.7 when using LIMIT/OFFSET request
80
                        ->scalarNode('order_parameter_name')->defaultValue('order')->cannotBeEmpty()->info('The name of the query parameter to order results.')->end()
81
                        ->arrayNode('pagination')
82
                            ->canBeDisabled()
83
                            ->addDefaultsIfNotSet()
84
                            ->children()
85
                                ->booleanNode('enabled')->defaultTrue()->info('To enable or disable pagination for all resource collections by default.')->end()
86
                                ->booleanNode('client_enabled')->defaultFalse()->info('To allow the client to enable or disable the pagination.')->end()
87
                                ->booleanNode('client_items_per_page')->defaultFalse()->info('To allow the client to set the number of items per page.')->end()
88
                                ->integerNode('items_per_page')->defaultValue(30)->info('The default number of items per page.')->end()
89
                                ->integerNode('maximum_items_per_page')->defaultNull()->info('The maximum number of items per page.')->end()
90
                                ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
91
                                ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
92
                                ->scalarNode('items_per_page_parameter_name')->defaultValue('itemsPerPage')->cannotBeEmpty()->info('The name of the query parameter to set the number of items per page.')->end()
93
                            ->end()
94
                        ->end()
95
                    ->end()
96
                ->end()
97
98
                ->arrayNode('loader_paths')
99
                    ->addDefaultsIfNotSet()
100
                    ->children()
101
                        ->arrayNode('annotation')
102
                            ->prototype('scalar')->end()
103
                        ->end()
104
                        ->arrayNode('yaml')
105
                            ->prototype('scalar')->end()
106
                        ->end()
107
                        ->arrayNode('xml')
108
                            ->prototype('scalar')->end()
109
                        ->end()
110
                    ->end()
111
                ->end()
112
            ->end();
113
114
        $this->addExceptionToStatusSection($rootNode);
115
116
        $this->addFormatSection($rootNode, 'formats', [
117
            'jsonld' => ['mime_types' => ['application/ld+json']],
118
            'json' => ['mime_types' => ['application/json']], // Swagger support
119
            'html' => ['mime_types' => ['text/html']], // Swagger UI support
120
        ]);
121
        $this->addFormatSection($rootNode, 'error_formats', [
122
            'jsonproblem' => ['mime_types' => ['application/problem+json']],
123
            'jsonld' => ['mime_types' => ['application/ld+json']],
124
        ]);
125
126
        return $treeBuilder;
127
    }
128
129
    /**
130
     * Adds an exception to status section.
131
     *
132
     * @param ArrayNodeDefinition $rootNode
133
     *
134
     * @throws InvalidConfigurationException
135
     */
136
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode)
137
    {
138
        $rootNode
139
            ->children()
140
                ->arrayNode('exception_to_status')
141
                    ->defaultValue([
142
                        ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
143
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
144
                    ])
145
                    ->info('The list of exceptions mapped to their HTTP status code.')
146
                    ->normalizeKeys(false)
147
                    ->useAttributeAsKey('exception_class')
148
                    ->beforeNormalization()
149
                        ->ifArray()
150
                        ->then(function (array $exceptionToStatus) {
151
                            foreach ($exceptionToStatus as &$httpStatusCode) {
152
                                if (is_int($httpStatusCode)) {
153
                                    continue;
154
                                }
155
156
                                if (defined($httpStatusCodeConstant = sprintf('%s::%s', Response::class, $httpStatusCode))) {
157
                                    $httpStatusCode = constant($httpStatusCodeConstant);
158
                                }
159
                            }
160
161
                            return $exceptionToStatus;
162
                        })
163
                    ->end()
164
                    ->prototype('integer')->end()
165
                    ->validate()
166
                        ->ifArray()
167
                        ->then(function (array $exceptionToStatus) {
168
                            foreach ($exceptionToStatus as $httpStatusCode) {
169
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
170
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
171
                                }
172
                            }
173
174
                            return $exceptionToStatus;
175
                        })
176
                    ->end()
177
                ->end()
178
            ->end();
179
    }
180
181
    /**
182
     * Adds a format section.
183
     *
184
     * @param ArrayNodeDefinition $rootNode
185
     * @param string              $key
186
     * @param array               $defaultValue
187
     */
188
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue)
189
    {
190
        $rootNode
191
            ->children()
192
                ->arrayNode($key)
193
                    ->defaultValue($defaultValue)
194
                    ->info('The list of enabled formats. The first one will be the default.')
195
                    ->normalizeKeys(false)
196
                    ->useAttributeAsKey('format')
197
                    ->beforeNormalization()
198
                        ->ifArray()
199
                        ->then(function ($v) {
200
                            foreach ($v as $format => $value) {
201
                                if (isset($value['mime_types'])) {
202
                                    continue;
203
                                }
204
205
                                $v[$format] = ['mime_types' => $value];
206
                            }
207
208
                            return $v;
209
                        })
210
                    ->end()
211
                    ->prototype('array')
212
                        ->children()
213
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
214
                        ->end()
215
                    ->end()
216
                ->end()
217
            ->end();
218
    }
219
}
220