Completed
Push — master ( bf39f0...8e985c )
by Guilh
11:33 queued 02:42
created

FOSRestExtension   C

Complexity

Total Complexity 76

Size/Duplication

Total Lines 411
Duplicated Lines 24.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 85.66%

Importance

Changes 11
Bugs 2 Features 5
Metric Value
wmc 76
c 11
b 2
f 5
lcom 1
cbo 8
dl 101
loc 411
ccs 245
cts 286
cp 0.8566
rs 5.488

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 4 1
B load() 0 45 3
A loadForm() 0 7 2
A loadAccessDeniedListener() 0 15 4
A loadAllowedMethodsListener() 13 13 3
B loadBodyListener() 0 25 4
A loadFormatListener() 13 16 4
C loadVersioning() 12 30 7
A loadParamFetcherListener() 15 15 4
B loadBodyConverter() 0 18 5
F loadView() 36 78 15
C loadException() 0 41 8
B loadSerializer() 12 21 6
A loadZoneMatcherListener() 0 18 3
B createZoneRequestMatcher() 0 22 4
A testExceptionExists() 0 6 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like FOSRestExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FOSRestExtension, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Symfony\Component\HttpFoundation\Response;
21
22
class FOSRestExtension extends Extension
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getConfiguration(array $config, ContainerBuilder $container)
28
    {
29
        return new Configuration($container->getParameter('kernel.debug'));
30
    }
31
32
    /**
33 57
     * Loads the services based on your application configuration.
34
     *
35 57
     * @param array            $configs
36 57
     * @param ContainerBuilder $container
37
     *
38 52
     * @throws \InvalidArgumentException
39 52
     * @throws \LogicException
40 52
     */
41 52
    public function load(array $configs, ContainerBuilder $container)
42 52
    {
43
        $configuration = new Configuration($container->getParameter('kernel.debug'));
44 52
        $config = $this->processConfiguration($configuration, $configs);
45 52
46 52
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
47
        $loader->load('view.xml');
48 52
        $loader->load('routing.xml');
49 52
        $loader->load('request.xml');
50 52
        $loader->load('serializer.xml');
51
52
        $container->getDefinition('fos_rest.routing.loader.controller')->replaceArgument(4, $config['routing_loader']['default_format']);
53 52
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(4, $config['routing_loader']['default_format']);
54 52
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(4, $config['routing_loader']['default_format']);
55
56 52
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(2, $config['routing_loader']['include_format']);
57 52
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(2, $config['routing_loader']['include_format']);
58 52
        $container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(3, $config['routing_loader']['include_format']);
59 52
60 52
        // The validator service alias is only set if validation is enabled for the request body converter
61
        $validator = $config['service']['validator'];
62 52
        unset($config['service']['validator']);
63 52
64 50
        foreach ($config['service'] as $key => $service) {
65 50
            if (null !== $service) {
66
                $container->setAlias('fos_rest.'.$key, $service);
67 50
            }
68 50
        }
69 50
70 50
        $this->loadForm($config, $loader, $container);
71 50
        $this->loadException($config, $loader, $container);
72 50
        $this->loadBodyConverter($config, $validator, $loader, $container);
73 50
        $this->loadView($config, $loader, $container);
74
75
        $this->loadBodyListener($config, $loader, $container);
76 50
        $this->loadFormatListener($config, $loader, $container);
77 50
        $this->loadVersioning($config, $loader, $container);
78
        $this->loadParamFetcherListener($config, $loader, $container);
79 52
        $this->loadAllowedMethodsListener($config, $loader, $container);
80
        $this->loadAccessDeniedListener($config, $loader, $container);
81 52
        $this->loadZoneMatcherListener($config, $loader, $container);
82 1
83 1
        // Needs RequestBodyParamConverter and View Handler loaded.
84 1
        $this->loadSerializer($config, $container);
85 52
    }
86
87 50
    private function loadForm(array $config, XmlFileLoader $loader, ContainerBuilder $container)
88
    {
89 50
        if (!empty($config['disable_csrf_role'])) {
90
            $loader->load('forms.xml');
91
            $container->getDefinition('fos_rest.form.extension.csrf_disable')->replaceArgument(1, $config['disable_csrf_role']);
92
        }
93
    }
94
95
    private function loadAccessDeniedListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
96
    {
97
        if ($config['access_denied_listener']['enabled'] && !empty($config['access_denied_listener']['formats'])) {
98
            $loader->load('access_denied_listener.xml');
99
100
            $service = $container->getDefinition('fos_rest.access_denied_listener');
101 50
102
            if (!empty($config['access_denied_listener']['service'])) {
103 50
                $service->clearTag('kernel.event_subscriber');
104
            }
105 50
106 2
            $service->replaceArgument(0, $config['access_denied_listener']['formats']);
107
            $service->replaceArgument(1, $config['unauthorized_challenge']);
108
        }
109
    }
110
111 2 View Code Duplication
    private function loadAllowedMethodsListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113 2
        if ($config['allowed_methods_listener']['enabled']) {
114 2
            if (!empty($config['allowed_methods_listener']['service'])) {
115 50
                $service = $container->getDefinition('fos_rest.allowed_methods_listener');
116
                $service->clearTag('kernel.event_listener');
117 50
            }
118
119 50
            $loader->load('allowed_methods_listener.xml');
120 49
121
            $container->getDefinition('fos_rest.allowed_methods_loader')->replaceArgument(1, $config['cache_dir']);
122 49
        }
123
    }
124 49
125
    private function loadBodyListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
126
    {
127
        if ($config['body_listener']['enabled']) {
128 49
            $loader->load('body_listener.xml');
129 49
130
            $service = $container->getDefinition('fos_rest.body_listener');
131 49
132
            if (!empty($config['body_listener']['service'])) {
133 49
                $service->clearTag('kernel.event_listener');
134
            }
135 49
136 3
            $service->replaceArgument(1, $config['body_listener']['throw_exception_on_unsupported_content_type']);
137 3
            $service->addMethodCall('setDefaultFormat', array($config['body_listener']['default_format']));
138 3
139 3
            $container->getDefinition('fos_rest.decoder_provider')->replaceArgument(1, $config['body_listener']['decoders']);
140 49
141 50
            $arrayNormalizer = $config['body_listener']['array_normalizer'];
142
143 50
            if (null !== $arrayNormalizer['service']) {
144
                $bodyListener = $container->getDefinition('fos_rest.body_listener');
145 50
                $bodyListener->addArgument(new Reference($arrayNormalizer['service']));
146 6
                $bodyListener->addArgument($arrayNormalizer['forms']);
147
            }
148 6
        }
149
    }
150
151
    private function loadFormatListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
152
    {
153 6 View Code Duplication
        if ($config['format_listener']['enabled'] && !empty($config['format_listener']['rules'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154 6
            $loader->load('format_listener.xml');
155 6
156 6
            if (!empty($config['format_listener']['service'])) {
157 6
                $service = $container->getDefinition('fos_rest.format_listener');
158 50
                $service->clearTag('kernel.event_listener');
159
            }
160 50
161
            $container->setParameter(
162 50
                'fos_rest.format_listener.rules',
163 2
                $config['format_listener']['rules']
164
            );
165 2
        }
166 2
    }
167
168 2
    private function loadVersioning(array $config, XmlFileLoader $loader, ContainerBuilder $container)
169 2
    {
170 2
        if (!empty($config['versioning']['enabled'])) {
171 2
            $loader->load('versioning.xml');
172 2
173 2
            $versionListener = $container->getDefinition('fos_rest.versioning.listener');
174 2
            $versionListener->replaceArgument(2, $config['versioning']['default_version']);
175 2
176 2
            $resolvers = [];
177 2 View Code Duplication
            if ($config['versioning']['resolvers']['query']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178 2
                $resolvers['query'] = $container->getDefinition('fos_rest.versioning.query_parameter_resolver');
179 2
                $resolvers['query']->replaceArgument(0, $config['versioning']['resolvers']['query']['parameter_name']);
180 2
            }
181 View Code Duplication
            if ($config['versioning']['resolvers']['custom_header']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182 2
                $resolvers['custom_header'] = $container->getDefinition('fos_rest.versioning.header_resolver');
183 2
                $resolvers['custom_header']->replaceArgument(0, $config['versioning']['resolvers']['custom_header']['header_name']);
184 2
            }
185 2 View Code Duplication
            if ($config['versioning']['resolvers']['media_type']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186 2
                $resolvers['media_type'] = $container->getDefinition('fos_rest.versioning.media_type_resolver');
187 2
                $resolvers['media_type']->replaceArgument(0, $config['versioning']['resolvers']['media_type']['regex']);
188 2
            }
189 50
190
            $chainResolver = $container->getDefinition('fos_rest.versioning.chain_resolver');
191 50
            foreach ($config['versioning']['guessing_order'] as $resolver) {
192
                if (isset($resolvers[$resolver])) {
193 50
                    $chainResolver->addMethodCall('addResolver', [$resolvers[$resolver]]);
194 4
                }
195
            }
196 4
        }
197
    }
198
199 View Code Duplication
    private function loadParamFetcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201 4
        if ($config['param_fetcher_listener']['enabled']) {
202 1
            $loader->load('param_fetcher_listener.xml');
203 1
204 4
            if (!empty($config['param_fetcher_listener']['service'])) {
205 50
                $service = $container->getDefinition('fos_rest.param_fetcher_listener');
206
                $service->clearTag('kernel.event_listener');
207 50
            }
208
209 50
            if ($config['param_fetcher_listener']['force']) {
210
                $container->getDefinition('fos_rest.param_fetcher_listener')->replaceArgument(1, true);
211
            }
212
        }
213 50
    }
214 2
215
    private function loadBodyConverter(array $config, $validator, XmlFileLoader $loader, ContainerBuilder $container)
216 2
    {
217 2
        if (empty($config['body_converter'])) {
218 2
            return;
219 2
        }
220
221 50
        if (!empty($config['body_converter']['enabled'])) {
222 2
            $loader->load('request_body_param_converter.xml');
223 2
224 50
            if (!empty($config['body_converter']['validation_errors_argument'])) {
225
                $container->getDefinition('fos_rest.converter.request_body')->replaceArgument(4, $config['body_converter']['validation_errors_argument']);
226 50
            }
227
        }
228 50
229 1
        if (!empty($config['body_converter']['validate'])) {
230 1
            $container->setAlias('fos_rest.validator', $validator);
231
        }
232 1
    }
233 1
234 1
    private function loadView(array $config, XmlFileLoader $loader, ContainerBuilder $container)
235
    {
236 1
        if (!empty($config['view']['jsonp_handler'])) {
237
            $handler = new DefinitionDecorator($config['service']['view_handler']);
238 1
            $handler->setPublic(true);
239 1
240 1
            $jsonpHandler = new Reference('fos_rest.view_handler.jsonp');
241 1
            $handler->addMethodCall('registerHandler', ['jsonp', [$jsonpHandler, 'createResponse']]);
242
            $container->setDefinition('fos_rest.view_handler', $handler);
243 50
244 2
            $container->getDefinition('fos_rest.view_handler.jsonp')->replaceArgument(0, $config['view']['jsonp_handler']['callback_param']);
245
246 2
            if (empty($config['view']['mime_types']['jsonp'])) {
247
                $config['view']['mime_types']['jsonp'] = $config['view']['jsonp_handler']['mime_type'];
248
            }
249
        }
250
251 2 View Code Duplication
        if ($config['view']['mime_types']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252 2
            $loader->load('mime_type_listener.xml');
253
254 50
            if (!empty($config['mime_type_listener']['service'])) {
255 8
                $service = $container->getDefinition('fos_rest.mime_type_listener');
256 8
                $service->clearTag('kernel.event_listener');
257
            }
258 8
259
            $container->getDefinition('fos_rest.mime_type_listener')->replaceArgument(0, $config['view']['mime_types']['formats']);
260
        }
261
262 8 View Code Duplication
        if ($config['view']['view_response_listener']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
263 8
            $loader->load('view_response_listener.xml');
264
            $service = $container->getDefinition('fos_rest.view_response_listener');
265 50
266 50
            if (!empty($config['view_response_listener']['service'])) {
267 50
                $service->clearTag('kernel.event_listener');
268 50
            }
269 50
270 50
            $service->replaceArgument(1, $config['view']['view_response_listener']['force']);
271 50
        }
272 50
273 50
        $formats = [];
274 50 View Code Duplication
        foreach ($config['view']['formats'] as $format => $enabled) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275 50
            if ($enabled) {
276
                $formats[$format] = false;
277 50
            }
278 50
        }
279 50 View Code Duplication
        foreach ($config['view']['templating_formats'] as $format => $enabled) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
            if ($enabled) {
281 50
                $formats[$format] = true;
282 50
            }
283 50
        }
284 50
285 50
        $container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(3, $formats);
286
        $container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(3, $formats);
287 50
        $container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(4, $formats);
288
289
        foreach ($config['view']['force_redirects'] as $format => $code) {
290
            if (true === $code) {
291 50
                $config['view']['force_redirects'][$format] = Response::HTTP_FOUND;
292 50
            }
293 50
        }
294
295 50 View Code Duplication
        if (!is_numeric($config['view']['failed_validation'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
296
            $config['view']['failed_validation'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['failed_validation']);
297
        }
298
299 50
        $defaultViewHandler = $container->getDefinition('fos_rest.view_handler.default');
300 50
        $defaultViewHandler->replaceArgument(4, $formats);
301 50
        $defaultViewHandler->replaceArgument(5, $config['view']['failed_validation']);
302 50
303 50 View Code Duplication
        if (!is_numeric($config['view']['empty_content'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
304
            $config['view']['empty_content'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['empty_content']);
305 52
        }
306
307 52
        $defaultViewHandler->replaceArgument(6, $config['view']['empty_content']);
308 16
        $defaultViewHandler->replaceArgument(7, $config['view']['serialize_null']);
309
        $defaultViewHandler->replaceArgument(8, $config['view']['force_redirects']);
310 16
        $defaultViewHandler->replaceArgument(9, $config['view']['default_engine']);
311
    }
312
313
    private function loadException(array $config, XmlFileLoader $loader, ContainerBuilder $container)
314
    {
315 16
        if ($config['exception']['enabled']) {
316
            $loader->load('exception_listener.xml');
317 16
318 4
            if (!empty($config['exception']['service'])) {
319 4
                $service = $container->getDefinition('fos_rest.exception_listener');
320
                $service->clearTag('kernel.event_subscriber');
321 16
            }
322 16
323 16
            if ($config['exception']['exception_controller']) {
324 16
                $container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, $config['exception']['exception_controller']);
325
            } elseif (isset($container->getParameter('kernel.bundles')['TwigBundle'])) {
326 16
                $container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, 'fos_rest.exception.twig_controller:showAction');
327 16
            }
328 16
329 16
            $container->getDefinition('fos_rest.exception.codes_map')
330 16
                ->replaceArgument(0, $config['exception']['codes']);
331 16
            $container->getDefinition('fos_rest.exception.messages_map')
332 16
                ->replaceArgument(0, $config['exception']['messages']);
333
334 52
            $container->getDefinition('fos_rest.exception.controller')
335 2
                ->replaceArgument(2, $config['exception']['debug']);
336
            $container->getDefinition('fos_rest.serializer.exception_normalizer.jms')
337
                ->replaceArgument(1, $config['exception']['debug']);
338
            $container->getDefinition('fos_rest.serializer.exception_normalizer.symfony')
339 2
                ->replaceArgument(1, $config['exception']['debug']);
340 51
        }
341
342 51
        foreach ($config['exception']['codes'] as $exception => $code) {
343 5
            if (!is_numeric($code)) {
344 50
                $config['exception']['codes'][$exception] = constant("\Symfony\Component\HttpFoundation\Response::$code");
345 50
            }
346
347 50
            $this->testExceptionExists($exception);
348
        }
349 50
350 50
        foreach ($config['exception']['messages'] as $exception => $message) {
351
            $this->testExceptionExists($exception);
352 50
        }
353 1
    }
354
355
    private function loadSerializer(array $config, ContainerBuilder $container)
356 1
    {
357 1
        $bodyConverter = $container->hasDefinition('fos_rest.converter.request_body') ? $container->getDefinition('fos_rest.converter.request_body') : null;
358
        $viewHandler = $container->getDefinition('fos_rest.view_handler.default');
359 50
360 1 View Code Duplication
        if (!empty($config['serializer']['version'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
361
            if ($bodyConverter) {
362
                $bodyConverter->replaceArgument(2, $config['serializer']['version']);
363 1
            }
364 1
            $viewHandler->addMethodCall('setExclusionStrategyVersion', array($config['serializer']['version']));
365
        }
366 50
367 50 View Code Duplication
        if (!empty($config['serializer']['groups'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
368
            if ($bodyConverter) {
369 50
                $bodyConverter->replaceArgument(1, $config['serializer']['groups']);
370
            }
371 50
            $viewHandler->addMethodCall('setExclusionStrategyGroups', array($config['serializer']['groups']));
372 2
        }
373 2
374
        $viewHandler->addMethodCall('setSerializeNullStrategy', array($config['serializer']['serialize_null']));
375 2
    }
376 2
377 2
    private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container)
378 2
    {
379 2
        if (!empty($config['zone'])) {
380 2
            $loader->load('zone_matcher_listener.xml');
381 2
            $zoneMatcherListener = $container->getDefinition('fos_rest.zone_matcher_listener');
382
383 2
            foreach ($config['zone'] as $zone) {
384 2
                $matcher = $this->createZoneRequestMatcher($container,
385 2
                    $zone['path'],
386 50
                    $zone['host'],
387
                    $zone['methods'],
388 2
                    $zone['ips']
389
                );
390 2
391
                $zoneMatcherListener->addMethodCall('addRequestMatcher', array($matcher));
392
            }
393
        }
394 2
    }
395 2
396
    private function createZoneRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = array(), $ip = null)
397
    {
398 2
        if ($methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
399 2
            $methods = array_map('strtoupper', (array) $methods);
400 2
        }
401 2
402
        $serialized = serialize(array($path, $host, $methods, $ip));
403
        $id = 'fos_rest.zone_request_matcher.'.md5($serialized).sha1($serialized);
404 2
405 2
        // only add arguments that are necessary
406
        $arguments = array($path, $host, $methods, $ip);
407
        while (count($arguments) > 0 && !end($arguments)) {
408 2
            array_pop($arguments);
409
        }
410
411
        $container
412
            ->setDefinition($id, new DefinitionDecorator('fos_rest.zone_request_matcher'))
413
            ->setArguments($arguments)
414
        ;
415
416
        return new Reference($id);
417
    }
418 7
419
    /**
420 7
     * Checks if an exception is loadable.
421 2
     *
422
     * @param string $exception Class to test
423 5
     *
424
     * @throws \InvalidArgumentException if the class was not found.
425
     */
426
    private function testExceptionExists($exception)
427
    {
428
        if (!is_subclass_of($exception, \Exception::class) && !is_a($exception, \Exception::class, true)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Exception::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
429
            throw new \InvalidArgumentException("FOSRestBundle exception mapper: Could not load class '$exception' or the class does not extend from '\Exception'. Most probably this is a configuration problem.");
430
        }
431
    }
432
}
433