Completed
Push — feature/rewrite ( f6b662...4d09af )
by Alexandre
01:59
created

BlackPageExtension::remapParametersNamespaces()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 13
nc 6
nop 3
1
<?php
2
3
namespace Black\Bundle\PageBundle\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
                'application_service',
50
                'controller',
51
                'cqrs',
52
                'domain_event',
53
                'dto',
54
                'infrastructure_service',
55
                'specification',
56
            ] as $basename
57
        ) {
58
            $loader->load(sprintf('%s.xml', $basename));
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @return string
66
     */
67
    public function getAlias()
68
    {
69
        return 'black_page';
70
    }
71
72
    /**
73
     * @param array            $config
74
     * @param ContainerBuilder $container
75
     * @param array            $map
76
     */
77
    protected function remapParameters(array $config, ContainerBuilder $container, array $map)
78
    {
79
        foreach ($map as $name => $paramName) {
80
            if (array_key_exists($name, $config)) {
81
                $container->setParameter($paramName, $config[$name]);
82
            }
83
        }
84
    }
85
86
    /**
87
     * @param array            $config
88
     * @param ContainerBuilder $container
89
     * @param array            $namespaces
90
     */
91
    protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
92
    {
93
        foreach ($namespaces as $ns => $map) {
94
            if ($ns) {
95
                if (!array_key_exists($ns, $config)) {
96
                    continue;
97
                }
98
                $namespaceConfig = $config[$ns];
99
            } else {
100
                $namespaceConfig = $config;
101
            }
102
103
            if (is_array($map)) {
104
                $this->remapParameters($namespaceConfig, $container, $map);
105
            } else {
106
                foreach ($namespaceConfig as $name => $value) {
107
                    $container->setParameter(sprintf($map, $name), $value);
108
                }
109
            }
110
        }
111
    }
112
}
113