UserProviderCompilerPass   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 77
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 3
B processRepository() 0 22 4
A checkRepositoryValidity() 0 16 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   https://opensource.org/licenses/MIT The MIT License (MIT)
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 5
    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 5
            ],
35
            [
36
                'serviceId'        => 'gm_redmine_user_provider.factory.user',
37
                'defaultServiceId' => 'gm_redmine_user_provider.factory.user.default'
38
            ]
39
        ];
40 5
        foreach ($defaultDependencies as $dependency) {
41 5
            if (!$container->has($dependency['serviceId'])) {
42 5
                $container->setAlias($dependency['serviceId'], $dependency['defaultServiceId']);
43
            }
44
        }
45
46 5
        $this->processRepository($container);
47 3
    }
48
49
    /**
50
     * Look for configured user repository, to inject it into user provider service
51
     *
52
     * @param ContainerBuilder $container
53
     */
54 5
    private function processRepository(ContainerBuilder $container)
55
    {
56 5
        $repositories = [];
57 5
        foreach (array_keys($container->findTaggedServiceIds('gm_redmine_user_provider.user_repository')) as $id) {
58 4
            $repositories[] = $id;
59
        }
60 5
        if (count($repositories) > 1) {
61 1
            throw new \InvalidArgumentException(
62
                sprintf(
63 1
                    'You cannot have multiple services tagged as "%s"',
64 1
                    'gm_redmine_user_provider.user_repository'
65
                )
66
            );
67
        }
68 4
        if (count($repositories) == 1) {
69 3
            $repositoryId = $repositories[0];
70 3
            $this->checkRepositoryValidity($container, $repositoryId);
71
72 2
            $definition = $container->getDefinition('gm_redmine_user_provider.provider');
73 2
            $definition->addMethodCall('setUserRepository', array(new Reference($repositoryId)));
74
        }
75 3
    }
76
77
    /**
78
     * Control if tagged repository is valid
79
     *
80
     * @param ContainerBuilder $container
81
     * @param string           $repositoryId
82
     */
83 3
    private function checkRepositoryValidity(ContainerBuilder $container, string $repositoryId)
84
    {
85 3
        $repositoryDefinition = $container->getDefinition($repositoryId);
86 3
        $className            = $container->getParameterBag()->resolveValue($repositoryDefinition->getClass());
87 3
        $reflection           = new \ReflectionClass($className);
88 3
        $repositoryInterface  = 'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface';
89 3
        if (!$reflection->implementsInterface($repositoryInterface)) {
90 1
            throw new \InvalidArgumentException(
91
                sprintf(
92 1
                    'The user repository %s should implement interface "%s"',
93
                    $repositoryId,
94 1
                    'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface'
95
                )
96
            );
97
        }
98 2
    }
99
}
100