Completed
Pull Request — master (#658)
by Magnus
02:24
created

RepositoryAliasPass::process()   C

Complexity

Conditions 9
Paths 15

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 51
rs 6.2727
cc 9
eloc 29
nc 15
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
4
5
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
6
use Doctrine\ORM\Mapping\ClassMetadata;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
class RepositoryAliasPass implements CompilerPassInterface
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    public function process(ContainerBuilder $container)
18
    {
19
        if (!$container->hasParameter('doctrine.entity_managers')) {
20
            return;
21
        }
22
23
        $entityManagers = $container->getParameter('doctrine.entity_managers');
24
        $customRepositories = [];
25
26
        foreach ($entityManagers as $name => $serviceName) {
27
            $metadataDriverService = sprintf('doctrine.orm.%s_metadata_driver', $name);
28
29
            if (!$container->has($metadataDriverService)) {
30
                continue;
31
            }
32
33
            /** @var MappingDriver $metadataDriver */
34
            $metadataDriver = $container->get($metadataDriverService);
35
            $entityClassNames = $metadataDriver->getAllClassNames();
36
37
            foreach ($entityClassNames as $entityClassName) {
38
                $classMetadata = new ClassMetadata($entityClassName);
39
                $metadataDriver->loadMetadataForClass($entityClassName, $classMetadata);
40
41
                if ($classMetadata->customRepositoryClassName) {
42
                    $customRepositories[$classMetadata->customRepositoryClassName][] = [
43
                        0 => $entityClassName,
44
                        1 => $name,
45
                    ];
46
                }
47
            }
48
49
            foreach ($customRepositories as $repositoryClass => $entities) {
50
                if ($container->has($repositoryClass)) {
51
                    continue;
52
                }
53
54
                if (count($entities) === 1) {
55
                    $definition = new Definition($repositoryClass);
56
                    $definition->setFactory(array(
57
                        new Reference('doctrine'),
58
                        'getRepository'
59
                    ));
60
                    $definition->setArguments($entities[0]);
61
                    $definition->setShared(false);
62
63
                    $container->setDefinition($repositoryClass, $definition);
64
                }
65
            }
66
        }
67
    }
68
}
69