Completed
Pull Request — master (#67)
by Ross
04:58
created

TacticianExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 0
loc 57
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadInternal() 0 7 1
A getAlias() 0 4 1
A configureSecurity() 0 8 3
A configureCommandSecurityVoter() 0 14 2
1
<?php
2
3
namespace League\Tactician\Bundle\DependencyInjection;
4
5
use League\Tactician\Bundle\Security\Voter\HandleCommandVoter;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Loader;
10
use Symfony\Component\DependencyInjection\Reference;
11
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
12
13
class TacticianExtension extends ConfigurableExtension
14
{
15
    /**
16
     * Configures the passed container according to the merged configuration.
17
     *
18
     * @param array $mergedConfig
19
     * @param ContainerBuilder $container
20
     */
21 66
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
22
    {
23 66
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services'));
24 66
        $loader->load('services.yml');
25
26 66
        $this->configureSecurity($mergedConfig, $container);
27 66
    }
28
29 69
    public function getAlias()
30
    {
31 69
        return 'tactician';
32
    }
33
34
    /**
35
     * Configure the security voter if the security middleware is loaded.
36
     *
37
     * @param array $mergedConfig
38
     * @param ContainerBuilder $container
39
     */
40 66
    private function configureSecurity(array $mergedConfig, ContainerBuilder $container)
41
    {
42 66
        foreach ($mergedConfig['commandbus'] as $commandBusConfig) {
43 66
            if (in_array('tactician.middleware.security', $commandBusConfig['middleware'])) {
44 66
                return $this->configureCommandSecurityVoter($mergedConfig, $container);
45
            }
46
        }
47 42
    }
48
49
    /**
50
     * Configure the security voter.
51
     *
52
     * @param array $mergedConfig
53
     * @param ContainerBuilder $container
54
     */
55 24
    private function configureCommandSecurityVoter(array $mergedConfig, ContainerBuilder $container)
56
    {
57 24
        if (!$container->has('tactician.middleware.security_voter')) {
58 24
            $definition = new Definition(
59 24
                HandleCommandVoter::class,
60
                [
61 24
                    new Reference('security.access.decision_manager'),
62 24
                    $mergedConfig['security']
63
                ]
64
            );
65 24
            $definition->addTag('security.voter');
66 24
            $container->setDefinition('tactician.middleware.security_voter', $definition);
67
        }
68 24
    }
69
}
70