Completed
Push — feature/rewrite ( 88e863...f6b662 )
by Alexandre
01:55
created

BlackPageExtension   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 101
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 44 4
A getAlias() 0 4 1
A remapParameters() 0 8 3
B remapParametersNamespaces() 0 21 6
1
<?php
2
3
namespace Black\Bundle\PageBundle\Application\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Processor;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
11
/**
12
 * Class BlackPageExtension
13
 */
14
class BlackPageExtension extends Extension
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function load(array $configs, ContainerBuilder $container)
20
    {
21
        $processor = new Processor();
22
        $configuration = new Configuration($this->getAlias());
23
        $config = $processor->processConfiguration($configuration, $configs);
24
25
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../Resources/config'));
26
27
        if (!isset($config['db_driver'])) {
28
            throw new \InvalidArgumentException('You must provide the black_page.db_driver configuration');
29
        }
30
31
        try {
32
            $loader->load(sprintf('%s.xml', $config['db_driver']));
33
        } catch (\InvalidArgumentException $e) {
34
            throw new \InvalidArgumentException(
35
                sprintf('The db_driver "%s" is not supported by engine', $config['db_driver'])
36
            );
37
        }
38
39
        $container->setParameter($this->getAlias() . '.backend_type_' . $config['db_driver'], true);
40
41
        $this->remapParametersNamespaces($config, $container, [
42
            '' => [
43
                'page_class' => 'black_page.webpage.model.class',
44
            ]
45
        ]);
46
47
        foreach (
48
            [
49
                'command',
50
                'application_service',
51
                'controller',
52
                'cqrs',
53
                'domain_event',
54
                'dto',
55
                'form',
56
                'infrastructure_service',
57
                'specification',
58
            ] as $basename
59
        ) {
60
            $loader->load(sprintf('%s.xml', $basename));
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @return string
68
     */
69
    public function getAlias()
70
    {
71
        return 'black_page';
72
    }
73
74
    /**
75
     * @param array            $config
76
     * @param ContainerBuilder $container
77
     * @param array            $map
78
     */
79
    protected function remapParameters(array $config, ContainerBuilder $container, array $map)
80
    {
81
        foreach ($map as $name => $paramName) {
82
            if (array_key_exists($name, $config)) {
83
                $container->setParameter($paramName, $config[$name]);
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param array            $config
90
     * @param ContainerBuilder $container
91
     * @param array            $namespaces
92
     */
93
    protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
94
    {
95
        foreach ($namespaces as $ns => $map) {
96
            if ($ns) {
97
                if (!array_key_exists($ns, $config)) {
98
                    continue;
99
                }
100
                $namespaceConfig = $config[$ns];
101
            } else {
102
                $namespaceConfig = $config;
103
            }
104
105
            if (is_array($map)) {
106
                $this->remapParameters($namespaceConfig, $container, $map);
107
            } else {
108
                foreach ($namespaceConfig as $name => $value) {
109
                    $container->setParameter(sprintf($map, $name), $value);
110
                }
111
            }
112
        }
113
    }
114
}
115