Completed
Push — master ( 25d51e...4712d7 )
by Guillaume
02:08
created

GmRedmineUserProviderExtension::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * File part of the Redmine User Provider bundle
4
 *
5
 * @category  SymfonyBundle
6
 * @package   GMaissa.RedmineUserProviderBundle
7
 * @author    Guillaume Maïssa <[email protected]>
8
 * @copyright 2017 Guillaume Maïssa
9
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
10
 */
11
12
namespace GMaissa\RedmineUserProviderBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Processor;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
/**
21
 * Infrastructure UserBundle Dependency Injection Class
22
 */
23
class GmRedmineUserProviderExtension extends Extension
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 9
    public function load(array $configs, ContainerBuilder $container)
29
    {
30 9
        $processor     = new Processor();
31 9
        $configuration = new Configuration();
32 9
        $config        = $processor->processConfiguration($configuration, $configs);
33
34 9
        $this->registerParameters($container, $config);
35
36 8
        $this->loadServices($container, $config);
37 8
    }
38
39
    /**
40
     * Set parameters from bundle configuration
41
     *
42
     * @param ContainerBuilder $container
43
     * @param array            $config
44
     */
45 9
    private function registerParameters(ContainerBuilder $container, array $config)
46
    {
47 9
        $container->setParameter('gm_redmine_user_provider.redmine.url', $config['redmine']['url']);
48 9
        $container->setParameter(
49 9
            'gm_redmine_user_provider.redmine.allowed_domains',
50 9
            $config['redmine']['allowed_domains']
51
        );
52 9
        if (isset($config['user_class'])) {
53 9
            $reflection    = new \ReflectionClass($config['user_class']);
54 9
            $userInterface = '\Symfony\Component\Security\Core\User\UserInterface';
55 9
            if (!$reflection->implementsInterface($userInterface)) {
56 1
                throw new \InvalidArgumentException(
57
                    sprintf(
58 1
                        'The user class %s should implement interface "%s"',
59 1
                        $config['user_class'],
60
                        $userInterface
61
                    )
62
                );
63
            }
64
65 8
            $container->setParameter('gm_redmine_user_provider.user_class', $config['user_class']);
66
        }
67 8
    }
68
69
    /**
70
     * Load default and persistence services
71
     *
72
     * @param ContainerBuilder $container
73
     * @param array            $config
74
     */
75 8
    private function loadServices(ContainerBuilder $container, array $config)
76
    {
77 8
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
78
79
        // Load default services
80 8
        $loader->load('services.yml');
81
82
        // Load persistence services
83 8
        if (isset($config['persistence_driver']) && !is_null($config['persistence_driver'])) {
84 2
            $loader->load(sprintf('%s.yml', $config['persistence_driver']));
85
        }
86 8
    }
87
}
88