DoctrineORMServicesPass::process()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\DoctrineORMBridgeBundle\DependencyInjection\Compiler;
14
15
use BenGorUser\DoctrineORMBridge\Infrastructure\Persistence\DoctrineORMUserRepository;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * Register Doctrine ORM services compiler pass.
23
 *
24
 * Service declaration via PHP allows more
25
 * flexibility with customization extend users.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class DoctrineORMServicesPass implements CompilerPassInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function process(ContainerBuilder $container)
35
    {
36
        $config = $container->getParameter('bengor_user.config');
37
        foreach ($config['user_class'] as $key => $user) {
38
            if ('doctrine_orm' !== $user['persistence']) {
39
                continue;
40
            }
41
42
            $container->setDefinition(
43
                'bengor.user.infrastructure.persistence.' . $key . '_repository',
44
                (new Definition(
45
                    DoctrineORMUserRepository::class, [
46
                        $user['class'],
47
                    ]
48
                ))->setFactory([
49
                    new Reference('doctrine.orm.default_entity_manager'), 'getRepository',
50
                ])->setPublic(false)
51
            );
52
            $container->setAlias(
53
                'bengor_user.' . $key . '.repository',
54
                'bengor.user.infrastructure.persistence.' . $key . '_repository'
55
            );
56
        }
57
    }
58
}
59