NicofumaSwaggerExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 67
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 31 2
B createRequestMatcher() 0 27 5
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\DefinitionDecorator;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\HttpFoundation\RequestMatcher;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
13
class NicofumaSwaggerExtension extends Extension
14
{
15
    private $requestMatchers = [];
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function load(array $configs, ContainerBuilder $container)
21
    {
22
        $config = $this->processConfiguration(new Configuration(), $configs);
23
24
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
25
        $loader->load('services.yml');
26
27
        $mapDefinition = $container->getDefinition('swagger.validator_map');
28
        $map = [];
29
        foreach ($config['definitions'] as $key => $definition) {
30
            $matcher = $this->createRequestMatcher(
31
                $container,
32
                $definition['pattern']['path'],
33
                $definition['pattern']['host'],
34
                $definition['pattern']['methods'],
35
                $definition['pattern']['ips']
36
            );
37
38
            $schemaManagerId = 'swagger.schema_manager.'.$key;
39
            $schemaManager = $container->setDefinition($schemaManagerId, new DefinitionDecorator('swagger.schema_manager'));
40
            $schemaManager->replaceArgument(0, $definition['swagger_file']);
41
42
            $validatorId = 'swagger.validator.'.$key;
43
            $validator = $container->setDefinition($validatorId, new DefinitionDecorator('swagger.validator'));
44
            $validator->replaceArgument(1, new Reference($schemaManagerId));
45
            $validator->replaceArgument(2, $definition['strict']);
46
47
            $map[$validatorId] = $matcher;
48
        }
49
        $mapDefinition->replaceArgument(1, $map);
50
    }
51
52
    private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = [], $ip = null, array $attributes = [])
53
    {
54
        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...
55
            $methods = array_map('strtoupper', (array) $methods);
56
        }
57
58
        $serialized = serialize([$path, $host, $methods, $ip, $attributes]);
59
        $id = 'swagger.request_matcher.'.md5($serialized).sha1($serialized);
60
61
        if (isset($this->requestMatchers[$id])) {
62
            return $this->requestMatchers[$id];
63
        }
64
65
        // only add arguments that are necessary
66
        $arguments = [$path, $host, $methods, $ip, $attributes];
67
        while (count($arguments) > 0 && !end($arguments)) {
68
            array_pop($arguments);
69
        }
70
71
        $container
72
            ->register($id, RequestMatcher::class)
73
            ->setPublic(false)
74
            ->setArguments($arguments)
75
        ;
76
77
        return $this->requestMatchers[$id] = new Reference($id);
78
    }
79
}
80