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

EnumExtension::load()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8.3646

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 23
cts 28
cp 0.8214
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 48
nop 2
crap 8.3646
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