Completed
Pull Request — master (#4)
by Guillaume
03:35
created

UserProviderCompilerPass::process()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 25
cts 25
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 31
nc 24
nop 1
crap 7
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 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
        $repositories = [];
47 5
        foreach (array_keys($container->findTaggedServiceIds('gm_redmine_user_provider.user_repository')) as $id) {
48 4
            $repositories[] = $id;
49
        }
50 5
        if (count($repositories) > 1) {
51 1
            throw new \InvalidArgumentException(
52
                sprintf(
53 1
                    'You cannot have multiple services tagged as "%s"',
54 1
                    'gm_redmine_user_provider.user_repository'
55
                )
56
            );
57
        }
58 4
        if (count($repositories) == 1) {
59 3
            $repositoryId         = $repositories[0];
60 3
            $repositoryDefinition = $container->getDefinition($repositoryId);
61 3
            $className            = $container->getParameterBag()->resolveValue($repositoryDefinition->getClass());
62 3
            $reflection           = new \ReflectionClass($className);
63 3
            $repositoryInterface  = 'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface';
64 3
            if (!$reflection->implementsInterface($repositoryInterface)) {
65 1
                throw new \InvalidArgumentException(
66
                    sprintf(
67 1
                        'The user repository %s should implement interface "%s"',
68
                        $repositoryId,
69 1
                        'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface'
70
                    )
71
                );
72
            }
73
74 2
            $definition = $container->getDefinition('gm_redmine_user_provider.provider');
75 2
            $definition->addMethodCall('setUserRepository', array(new Reference($repositoryId)));
76
        }
77 3
    }
78
}
79