Completed
Push — master ( 979590...f7b9af )
by John
06:14
created

KleijnWebSwaggerExtension::load()   C

Complexity

Conditions 11
Paths 80

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 6.4824
c 0
b 0
f 0
cc 11
eloc 36
nc 80
nop 2

How to fix   Long Method    Complexity   

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 declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\DependencyInjection;
10
11
use KleijnWeb\PhpApi\Hydrator\DateTimeSerializer;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Loader;
15
use Symfony\Component\DependencyInjection\Reference;
16
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17
18
/**
19
 * @author John Kleijn <[email protected]>
20
 */
21
class KleijnWebSwaggerExtension extends Extension
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function load(array $configs, ContainerBuilder $container)
27
    {
28
        $config = $this->processConfiguration(new Configuration(), $configs);
29
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
30
        $loader->load('services.yml');
31
32
        if ($config['handle_exceptions']) {
33
            $loader->load('listener_exception.yml');
34
        }
35
36
        $container->setParameter('swagger.document.base_path', $config['document']['base_path']);
37
        $container->setParameter('phpapi.router_name', 'swagger');
38
39
        if (isset($config['document']['cache'])) {
40
            $resolverDefinition = $container->getDefinition('swagger.description.repository');
41
            $resolverDefinition->addArgument(new Reference($config['document']['cache']));
42
        }
43
        $responseFactory = $container->getDefinition('swagger.response.factory');
44
45
        if (isset($config['hydrator'])) {
46
            $container
47
                ->getDefinition('swagger.hydrator.class_name_resolver')
48
                ->replaceArgument(0, $config['hydrator']['namespaces']);
49
50
            $dateTimeSerializerDefinition = $container->getDefinition('swagger.hydrator.class_name_resolver');
51
            if (isset($config['hydrator']['date_formats'])) {
52
                foreach ($config['hydrator']['date_formats'] as $format) {
53
                    $predefinedFormat = DateTimeSerializer::class . "::FORMAT_{$format}";
54
                    if (defined($predefinedFormat)) {
55
                        $format = constant($predefinedFormat);
56
                    }
57
                    $dateTimeSerializerDefinition->addArgument(new Reference($format));
58
                }
59
            }
60
61
            $builderDefinition = $container->getDefinition('swagger.hydrator.processor.builder');
62
63
            if (isset($config['hydrator']['processors'])) {
64
                foreach ($config['hydrator']['processors'] as $processor) {
65
                    $builderDefinition->addMethodCall('add', new Reference($processor));
0 ignored issues
show
Documentation introduced by
new \Symfony\Component\D...n\Reference($processor) is of type object<Symfony\Component...ncyInjection\Reference>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
                }
67
            }
68
69
            $hydrator   = new Reference('swagger.hydrator');
70
            $definition = $container->getDefinition('swagger.request.processor');
71
            $definition->addArgument($hydrator);
72
            $responseFactory->replaceArgument(0, $hydrator);
73
        }
74
        if ($config['validate_responses']) {
75
            $responseFactory->addArgument(new Reference('swagger.request.validator'));
76
        }
77
        if ($config['ok_status_resolver']) {
78
            $responseFactory->addArgument(new Reference($config['ok_status_resolver']));
79
        }
80
81
        $container->setParameter('swagger.match_unsecured', $config['security']['match_unsecured']);
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getAlias(): string
88
    {
89
        return "swagger";
90
    }
91
}
92