Completed
Push — master ( 48b842...eabf80 )
by Maxime
01:29
created

ElaoEnumExtension::load()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 32
nop 2
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Symfony\Bundle\DependencyInjection;
12
13
use ApiPlatform\Core\JsonSchema\TypeFactory;
14
use Elao\Enum\Bridge\ApiPlatform\Core\JsonSchema\Type\ElaoEnumType;
15
use Elao\Enum\Bridge\Doctrine\DBAL\Types\TypesDumper;
16
use Elao\Enum\Bridge\Symfony\HttpKernel\Controller\ArgumentResolver\EnumValueResolver;
17
use Elao\Enum\Bridge\Symfony\Serializer\Normalizer\EnumNormalizer;
18
use Elao\Enum\Bridge\Symfony\Translation\Extractor\EnumExtractor;
19
use Symfony\Component\Config\FileLocator;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
22
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
23
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
24
25
class ElaoEnumExtension extends Extension implements PrependExtensionInterface
26
{
27
    public function prepend(ContainerBuilder $container)
28
    {
29
        $bundles = $container->getParameter('kernel.bundles');
30
        if (!isset($bundles['DoctrineBundle'])) {
31
            return;
32
        }
33
34
        $configs = $container->getExtensionConfig($this->getAlias());
35
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type null|object, but the function expects a object<Symfony\Component...ConfigurationInterface>.

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...
36
37
        if (!($types = $config['doctrine']['types'] ?? false)) {
38
            return;
39
        }
40
41
        $doctrineTypesConfig = [];
42
        foreach ($types as $class => $value) {
43
            $doctrineTypesConfig[$value['name']] = TypesDumper::getTypeClassname($class);
44
        }
45
46
        $container->prependExtensionConfig('doctrine', [
47
            'dbal' => [
48
                'types' => $doctrineTypesConfig,
49
            ],
50
        ]);
51
    }
52
53
    public function load(array $configs, ContainerBuilder $container)
54
    {
55
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
56
        $loader->load('services.xml');
57
58
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type null|object, but the function expects a object<Symfony\Component...ConfigurationInterface>.

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...
59
60
        if (!$this->isConfigEnabled($container, $config['argument_value_resolver'])) {
61
            $container->removeDefinition(EnumValueResolver::class);
62
        }
63
64
        if (!$this->isConfigEnabled($container, $config['serializer'])) {
65
            $container->removeDefinition(EnumNormalizer::class);
66
        }
67
68
        if (!$this->isConfigEnabled($container, $config['translation_extractor'])) {
69
            $container->removeDefinition(EnumExtractor::class);
70
        } else {
71
            $this->registerTranslationExtractorConfiguration($config['translation_extractor'], $container);
72
        }
73
74
        if (!class_exists(TypeFactory::class)) {
75
            $container->removeDefinition(ElaoEnumType::class);
76
        }
77
78
        if ($types = $config['doctrine']['types'] ?? false) {
79
            $container->setParameter('.elao_enum.doctrine_types', array_map(static function (string $k, array $v): array {
80
                return [$k, $v['type'], $v['name']];
81
            }, array_keys($types), $types));
82
        }
83
    }
84
85
    public function getNamespace()
86
    {
87
        return 'http://elao.com/schema/dic/elao_enum';
88
    }
89
90
    public function getXsdValidationBasePath()
91
    {
92
        return __DIR__ . '/../Resources/config/schema';
93
    }
94
95
    private function registerTranslationExtractorConfiguration(array $config, ContainerBuilder $container)
96
    {
97
        $definition = $container->getDefinition(EnumExtractor::class);
98
99
        $definition->replaceArgument(0, $config['paths']);
100
        $definition->replaceArgument(1, $config['domain']);
101
        $definition->replaceArgument(2, $config['filename_pattern']);
102
        $definition->replaceArgument(3, $config['ignore']);
103
    }
104
}
105