Completed
Pull Request — master (#443)
by Peter
03:33
created

EntityListenerPass::process()   C

Complexity

Conditions 14
Paths 34

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 6.5728
c 0
b 0
f 0
cc 14
eloc 30
nc 34
nop 1

How to fix   Long Method    Complexity   

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
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 * (c) Doctrine Project, Benjamin Eberlei <[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 Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
14
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * Class for Symfony bundles to register entity listeners
22
 *
23
 * @author Sander Marechal <[email protected]>
24
 */
25
class EntityListenerPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritDoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        $resolvers = $container->findTaggedServiceIds('doctrine.orm.entity_listener');
33
34
        foreach ($resolvers as $id => $tagAttributes) {
35
            foreach ($tagAttributes as $attributes) {
36
                $name = isset($attributes['entity_manager']) ? $attributes['entity_manager'] : $container->getParameter('doctrine.default_entity_manager');
37
                $entityManager = sprintf('doctrine.orm.%s_entity_manager', $name);
38
39
                if (!$container->hasDefinition($entityManager)) {
40
                    continue;
41
                }
42
43
                $resolverId = sprintf('doctrine.orm.%s_entity_listener_resolver', $name);
44
45
                if (!$container->has($resolverId)) {
46
                    continue;
47
                }
48
49
                $resolver = $container->findDefinition($resolverId);
50
51
                if (isset($attributes['entity']) && isset($attributes['event'])) {
52
                    $this->attachToListener($container, $name, $id, $attributes);
53
                }
54
55
                if (isset($attributes['lazy']) && $attributes['lazy']) {
56
                    $listener = $container->findDefinition($id);
57
58
                    if (!$listener->isPublic()) {
59
                        throw new InvalidArgumentException(sprintf('The service "%s" must be public as this entity listener is lazy-loaded.', $id));
60
                    }
61
62
                    if ($listener->isAbstract()) {
63
                        throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as this entity listener is lazy-loaded.', $id));
64
                    }
65
66
                    $interface = 'Doctrine\\Bundle\\DoctrineBundle\\Mapping\\EntityListenerServiceResolver';
67
                    $class = $resolver->getClass();
68
69
                    if (substr($class, 0, 1) === '%') {
70
                        // resolve container parameter first
71
                        $class = $container->getParameterBag()->resolveValue($resolver->getClass());
72
                    }
73
74
                    if (!is_a($class, $interface, true)) {
75
                        throw new InvalidArgumentException(
76
                            sprintf('Lazy-loaded entity listeners can only be resolved by a resolver implementing %s.', $interface)
77
                        );
78
                    }
79
80
                    $resolver->addMethodCall('registerService', array($listener->getClass(), $id));
81
                } else {
82
                    $resolver->addMethodCall('register', array(new Reference($id)));
83
                }
84
            }
85
        }
86
    }
87
88
    private function attachToListener(ContainerBuilder $container, $name, $id, array $attributes)
89
    {
90
        $listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $name);
91
92
        if (!$container->has($listenerId)) {
93
            return;
94
        }
95
96
        $serviceDef = $container->getDefinition($id);
97
98
        $args = array(
99
            $attributes['entity'],
100
            $serviceDef->getClass(),
101
            $attributes['event'],
102
        );
103
104
        if (isset($attributes['method'])) {
105
            $args[] = $attributes['method'];
106
        }
107
108
        $container->findDefinition($listenerId)->addMethodCall('addEntityListener', $args);
109
    }
110
}
111