Completed
Push — master ( 8a815b...25d51e )
by Guillaume
02:21
created

UserProviderCompilerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 45
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 39 5
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\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * User provider compiler pass
20
 *
21
 * Inject User repository in user provider if declared
22
 */
23
class UserProviderCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function process(ContainerBuilder $container)
29
    {
30
        $defaultDependencies = [
31
            [
32
                'serviceId'        => 'gm_redmine_user_provider.api.client',
33
                'defaultServiceId' => 'gm_redmine_user_provider.api.client.default'
34 4
            ],
35
            [
36
                'serviceId'        => 'gm_redmine_user_provider.factory.user',
37
                'defaultServiceId' => 'gm_redmine_user_provider.factory.user.default'
38
            ]
39
        ];
40 4
        foreach ($defaultDependencies as $dependency) {
41 4
            if (!$container->has($dependency['serviceId'])) {
42 4
                $container->setAlias($dependency['serviceId'], $dependency['defaultServiceId']);
43
            }
44
        }
45
46 4
        if ($container->hasParameter('gm_redmine_user_provider.user_repository_service')) {
47 3
            $serviceId            = $container->getParameter('gm_redmine_user_provider.user_repository_service');
48 3
            $repositoryDefinition = $container->getDefinition($serviceId);
49 2
            $className            = $container->getParameterBag()->resolveValue($repositoryDefinition->getClass());
50 2
            $reflection           = new \ReflectionClass($className);
51 2
            $repositoryInterface  = 'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface';
52 2
            if (!$reflection->implementsInterface($repositoryInterface)) {
53
                throw new \InvalidArgumentException(
54
                    sprintf(
55
                        'The user repository %s should implement interface "%s"',
56
                        $serviceId,
57
                        'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface'
58
                    )
59
                );
60
            }
61
62 2
            $definition = $container->getDefinition('gm_redmine_user_provider.provider');
63 2
            $definition->addMethodCall('setUserRepository', array(new Reference($serviceId)));
64 2
            $container->getParameterBag()->remove('gm_redmine_user_provider.user_repository_service');
65
        }
66 3
    }
67
}
68