Completed
Pull Request — master (#29)
by Yann
10:30
created

EnumExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 40
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C load() 0 34 8
1
<?php
2
3
namespace Yokai\Enum\Bridge\Symfony\Bundle\DependencyInjection;
4
5
use Symfony\Bundle\TwigBundle\TwigBundle;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
use Yokai\Enum\EnumInterface;
13
use Yokai\Enum\EnumRegistry;
14
15
/**
16
 * @author Yann Eugoné <[email protected]>
17
 */
18
class EnumExtension extends Extension
19
{
20
    /**
21
     * @inheritdoc
22
     */
23 5
    public function load(array $configs, ContainerBuilder $container)
24
    {
25 5
        $configuration = new Configuration();
26 5
        $config = $this->processConfiguration($configuration, $configs);
27
28 5
        $xmlLoader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
29 5
        $xmlLoader->load('enum.xml');
30
31 5
        $requiresForm = interface_exists(FormInterface::class);
32 5
        $requiresValidator = interface_exists(ValidatorInterface::class);
33 5
        $requiresTwig = class_exists(TwigBundle::class);
34
35 5
        if ($requiresForm) {
36 5
            $xmlLoader->load('form.xml');
37 5
            if (!$requiresValidator) {
38
                $container->removeDefinition('form_extention.type_guesser.enum');
39
            }
40 5
        }
41 5
        if ($requiresValidator) {
42 5
            $xmlLoader->load('validator.xml');
43 5
        }
44 5
        if ($requiresTwig) {
45 5
            $xmlLoader->load('twig.xml');
46 5
        }
47
48 5
        if ($config['enum_autoconfiguration'] && method_exists($container, 'registerForAutoconfiguration')) {
49
            $container->registerForAutoconfiguration(EnumInterface::class)
50
                ->addTag('enum');
51
        }
52
53 5
        if ($config['enum_registry_autoconfigurable']) {
54 4
            $container->setAlias(EnumRegistry::class, 'enum.registry');
55 4
        }
56 5
    }
57
}
58