Completed
Pull Request — master (#869)
by Markus
03:23
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.9019
c 0
b 0
f 0
cc 1
eloc 68
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('collection')
60
                    ->addDefaultsIfNotSet()
61
                    ->children()
62
                        ->scalarNode('order')->defaultNull()->info('The default order of results.')->end()
63
                        ->scalarNode('order_parameter_name')->defaultValue('order')->cannotBeEmpty()->info('The name of the query parameter to order results.')->end()
64
                        ->arrayNode('pagination')
65
                            ->canBeDisabled()
66
                            ->addDefaultsIfNotSet()
67
                            ->children()
68
                                ->booleanNode('enabled')->defaultTrue()->info('To enable or disable pagination for all resource collections by default.')->end()
69
                                ->booleanNode('client_enabled')->defaultFalse()->info('To allow the client to enable or disable the pagination.')->end()
70
                                ->booleanNode('client_items_per_page')->defaultFalse()->info('To allow the client to set the number of items per page.')->end()
71
                                ->integerNode('items_per_page')->defaultValue(30)->info('The default number of items per page.')->end()
72
                                ->integerNode('maximum_items_per_page')->defaultNull()->info('The maximum number of items per page.')->end()
73
                                ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
74
                                ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
75
                                ->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()
76
                            ->end()
77
                        ->end()
78
                    ->end()
79
                ->end()
80
81
                ->arrayNode('loader_paths')
82
                    ->addDefaultsIfNotSet()
83
                    ->children()
84
                        ->arrayNode('annotation')
85
                            ->prototype('scalar')->end()
86
                        ->end()
87
                        ->arrayNode('yaml')
88
                            ->prototype('scalar')->end()
89
                        ->end()
90
                        ->arrayNode('xml')
91
                            ->prototype('scalar')->end()
92
                        ->end()
93
                    ->end()
94
                ->end()
95
            ->end();
96
97
        $this->addExceptionToStatusSection($rootNode);
98
99
        $this->addFormatSection($rootNode, 'formats', [
100
            'jsonld' => ['mime_types' => ['application/ld+json']],
101
            'json' => ['mime_types' => ['application/json']], // Swagger support
102
            'html' => ['mime_types' => ['text/html']], // Swagger UI support
103
        ]);
104
        $this->addFormatSection($rootNode, 'error_formats', [
105
            'jsonproblem' => ['mime_types' => ['application/problem+json']],
106
            'jsonld' => ['mime_types' => ['application/ld+json']],
107
        ]);
108
109
        return $treeBuilder;
110
    }
111
112
    /**
113
     * Adds an exception to status section.
114
     *
115
     * @param ArrayNodeDefinition $rootNode
116
     *
117
     * @throws InvalidConfigurationException
118
     */
119
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode)
120
    {
121
        $rootNode
122
            ->children()
123
                ->arrayNode('exception_to_status')
124
                    ->defaultValue([
125
                        ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
126
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
127
                    ])
128
                    ->info('The list of exceptions mapped to their HTTP status code.')
129
                    ->normalizeKeys(false)
130
                    ->useAttributeAsKey('exception_class')
131
                    ->beforeNormalization()
132
                        ->ifArray()
133
                        ->then(function (array $exceptionToStatus) {
134
                            foreach ($exceptionToStatus as &$httpStatusCode) {
135
                                if (is_int($httpStatusCode)) {
136
                                    continue;
137
                                }
138
139
                                if (defined($httpStatusCodeConstant = sprintf('%s::%s', Response::class, $httpStatusCode))) {
140
                                    $httpStatusCode = constant($httpStatusCodeConstant);
141
                                }
142
                            }
143
144
                            return $exceptionToStatus;
145
                        })
146
                    ->end()
147
                    ->prototype('integer')->end()
148
                    ->validate()
149
                        ->ifArray()
150
                        ->then(function (array $exceptionToStatus) {
151
                            foreach ($exceptionToStatus as $httpStatusCode) {
152
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
153
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
154
                                }
155
                            }
156
157
                            return $exceptionToStatus;
158
                        })
159
                    ->end()
160
                ->end()
161
            ->end();
162
    }
163
164
    /**
165
     * Adds a format section.
166
     *
167
     * @param ArrayNodeDefinition $rootNode
168
     * @param string              $key
169
     * @param array               $defaultValue
170
     */
171
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue)
172
    {
173
        $rootNode
174
            ->children()
175
                ->arrayNode($key)
176
                    ->defaultValue($defaultValue)
177
                    ->info('The list of enabled formats. The first one will be the default.')
178
                    ->normalizeKeys(false)
179
                    ->useAttributeAsKey('format')
180
                    ->beforeNormalization()
181
                        ->ifArray()
182
                        ->then(function ($v) {
183
                            foreach ($v as $format => $value) {
184
                                if (isset($value['mime_types'])) {
185
                                    continue;
186
                                }
187
188
                                $v[$format] = ['mime_types' => $value];
189
                            }
190
191
                            return $v;
192
                        })
193
                    ->end()
194
                    ->prototype('array')
195
                        ->children()
196
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
197
                        ->end()
198
                    ->end()
199
                ->end()
200
            ->end();
201
    }
202
}
203