Completed
Push — master ( 0fc0f1...9f2d68 )
by Gerard
02:36
created

GbereSimpleAuthExtension::updateSecurityConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gbere\SimpleAuth\DependencyInjection;
6
7
use Exception;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
14
class GbereSimpleAuthExtension extends Extension implements PrependExtensionInterface
15
{
16
    private const TESTING_ROUTES = [
17
        ['path' => '^/gbere-auth-test-role-admin', 'role' => 'ROLE_ADMIN'],
18
        ['path' => '^/gbere-auth-test-role-user', 'role' => 'ROLE_USER'],
19
    ];
20
21
    /** @var array|null */
22
    private $securityConf;
23
24
    /**
25
     * @throws Exception
26
     */
27
    public function load(array $configs, ContainerBuilder $container): void
28
    {
29
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
30
        $loader->load('services.yaml');
31
32
        $configuration = $this->getConfiguration($configs, $container);
33
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of Symfony\Component\Depend...:processConfiguration() does only seem to accept Symfony\Component\Config...\ConfigurationInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

33
        $config = $this->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $configs);
Loading history...
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
34
    }
35
36
    public function prepend(ContainerBuilder $container): void
37
    {
38
        if ('test' === $container->getParameter('kernel.environment')) {
39
            $this->securityConf['access_control'] = self::TESTING_ROUTES;
40
        }
41
42
        $this->updateSecurityConfig($container);
43
    }
44
45
    private function updateSecurityConfig(ContainerBuilder $container): void
46
    {
47
        if (null === $this->securityConf) {
48
            return;
49
        }
50
51
        $extensionConfigsRefl = new \ReflectionProperty(ContainerBuilder::class, 'extensionConfigs');
52
        $extensionConfigsRefl->setAccessible(true);
53
        $extensionConfigs = $extensionConfigsRefl->getValue($container);
54
55
        foreach ($this->securityConf as $section => $configs) {
56
            if (isset($extensionConfigs['security'][0][$section])) {
57
                $extensionConfigs['security'][0][$section] = array_merge($configs, $extensionConfigs['security'][0][$section]);
58
            } else {
59
                $extensionConfigs['security'][0][$section] = $configs;
60
            }
61
        }
62
63
        $extensionConfigsRefl->setValue($container, $extensionConfigs);
64
    }
65
}
66