Test Failed
Push — master ( c4c2e8...dd2174 )
by Elijah
03:37
created

EloyekunlePermissionsExtension::loadPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 10
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
    public function load(array $configs, ContainerBuilder $container)
38
    {
39
        $processor = new Processor();
40
        $configuration = new Configuration();
41
42
        $config = $processor->processConfiguration($configuration, $configs);
43
44
        $loader = new XmlFileLoader(
45
            $container,
46
            new FileLocator(__DIR__.'/../Resources/config')
47
        );
48
49
        $loader->load('doctrine.xml');
50
        $container->setAlias(
51
            'eloyekunle_permissions.doctrine_registry',
52
            new Alias(self::$doctrineDrivers[$config['db_driver']]['registry'], false)
53
        );
54
        $container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true);
55
56
        if (isset(self::$doctrineDrivers[$config['db_driver']])) {
57
            $definition = $container->getDefinition('eloyekunle_permissions.object_manager');
58
            $definition->setFactory([new Reference('eloyekunle_permissions.doctrine_registry'), 'getManager']);
59
        }
60
61
        $this->remapParametersNamespaces(
62
            $config,
63
            $container,
64
            array(
65
                '' => array(
66
                    'db_driver' => 'eloyekunle_permissions.storage',
67
                    'role_class' => 'eloyekunle_permissions.model.role.class',
68
                ),
69
            )
70
        );
71
72
        $this->loadRole($config, $container, $loader);
73
74
        if (!empty($config['module'])) {
75
            $this->loadPermissions($config['module'], $container, $loader);
76
        }
77
    }
78
79
    /**
80
     * @param array            $config
81
     * @param ContainerBuilder $container
82
     * @param array            $map
83
     */
84
    protected function remapParameters(array $config, ContainerBuilder $container, array $map)
85
    {
86
        foreach ($map as $name => $paramName) {
87
            if (array_key_exists($name, $config)) {
88
                $container->setParameter($paramName, $config[$name]);
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param array            $config
95
     * @param ContainerBuilder $container
96
     * @param array            $namespaces
97
     */
98
    protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
99
    {
100
        foreach ($namespaces as $ns => $map) {
101
            if ($ns) {
102
                if (!array_key_exists($ns, $config)) {
103
                    continue;
104
                }
105
                $namespaceConfig = $config[$ns];
106
            } else {
107
                $namespaceConfig = $config;
108
            }
109
            if (is_array($map)) {
110
                $this->remapParameters($namespaceConfig, $container, $map);
111
            } else {
112
                foreach ($namespaceConfig as $name => $value) {
113
                    $container->setParameter(sprintf($map, $name), $value);
114
                }
115
            }
116
        }
117
    }
118
119
    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

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