Completed
Push — master ( b86d36...38ab4c )
by Elijah
04:17
created

EloyekunlePermissionsExtension::remapParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Processor;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\Alias;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
class EloyekunlePermissionsExtension extends Extension
23
{
24
    /**
25
     * @var array
26
     */
27
    private static $doctrineDrivers = array(
28
        'orm' => array(
29
            'registry' => 'doctrine',
30
            'tag' => 'doctrine.event_subscriber',
31
        ),
32
    );
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 4
    public function load(array $configs, ContainerBuilder $container)
38
    {
39 4
        $processor = new Processor();
40 4
        $configuration = new Configuration();
41
42 4
        $config = $processor->processConfiguration($configuration, $configs);
43
44 3
        $loader = new XmlFileLoader(
45 3
            $container,
46 3
            new FileLocator(__DIR__.'/../Resources/config')
47
        );
48
49 3
        $loader->load('doctrine.xml');
50 3
        $container->setAlias(
51 3
            'eloyekunle_permissions.doctrine_registry',
52 3
            new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false)
53
        );
54 3
        $container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true);
55
56 3
        if (isset(self::$doctrineDrivers[$config['db_driver']])) {
57 3
            $definition = $container->getDefinition('eloyekunle_permissions.object_manager');
58 3
            $definition->setFactory([new Reference('eloyekunle_permissions.doctrine_registry'), 'getManager']);
59
        }
60
61 3
        $this->remapParametersNamespaces($config, $container, [
62 3
                '' => [
63
                    'db_driver' => 'eloyekunle_permissions.storage',
64
                    'role_class' => 'eloyekunle_permissions.model.role.class',
65
                    'model_manager_name' => 'eloyekunle_permissions.model_manager_name',
66
                ],
67
            ]
68
        );
69
70 3
        $this->loadRole($config, $container, $loader);
71
72 3
        if (!empty($config['module'])) {
73
            $this->loadPermissions($config['module'], $container, $loader);
74
        }
75 3
    }
76
77
    /**
78
     * @param array            $config
79
     * @param ContainerBuilder $container
80
     * @param array            $map
81
     */
82 3
    protected function remapParameters(array $config, ContainerBuilder $container, array $map)
83
    {
84 3
        foreach ($map as $name => $paramName) {
85 3
            if (array_key_exists($name, $config)) {
86 3
                $container->setParameter($paramName, $config[$name]);
87
            }
88
        }
89 3
    }
90
91
    /**
92
     * @param array            $config
93
     * @param ContainerBuilder $container
94
     * @param array            $namespaces
95
     */
96 3
    protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
97
    {
98 3
        foreach ($namespaces as $ns => $map) {
99 3
            if ($ns) {
100
                if (!array_key_exists($ns, $config)) {
101
                    continue;
102
                }
103
                $namespaceConfig = $config[$ns];
104
            } else {
105 3
                $namespaceConfig = $config;
106
            }
107 3
            if (is_array($map)) {
108 3
                $this->remapParameters($namespaceConfig, $container, $map);
109
            } else {
110
                foreach ($namespaceConfig as $name => $value) {
111 3
                    $container->setParameter(sprintf($map, $name), $value);
112
                }
113
            }
114
        }
115 3
    }
116
117 3
    private function loadRole(array $config, ContainerBuilder $container, XmlFileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

117
    private function loadRole(/** @scrutinizer ignore-unused */ array $config, ContainerBuilder $container, XmlFileLoader $loader)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119 3
        $loader->load('role.xml');
120 3
        $loader->load('doctrine_role.xml');
121
122 3
        $container->setAlias('eloyekunle_permissions.role_manager', new Alias('eloyekunle_permissions.role_manager.default', true)
123
        );
124 3
    }
125
126
    private function loadPermissions(array $config, ContainerBuilder $container, XmlFileLoader $loader)
127
    {
128
        $loader->load('permissions.xml');
129
130
        $this->remapParametersNamespaces(
131
            $config,
132
            $container,
133
            [
134
                '' => [
135
                    'definitions_path' => 'eloyekunle_permissions.module.definitions_path',
136
                ],
137
            ]
138
        );
139
    }
140
}
141