Completed
Push — master ( d8d25b...3f99c9 )
by Quentin
59s
created

DoctrineEventProxy::postLoad()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Majora\Framework\Loader\Bridge\Doctrine;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Majora\Framework\Loader\LazyLoaderInterface;
7
use Majora\Framework\Model\EntityCollection;
8
use Majora\Framework\Model\LazyPropertiesInterface;
9
10
/**
11
 * Proxy class to dispatch Doctrine events to proper loaders only
12
 * (Doctrine notify all listeners at each loaded entities, so we have to proxy it here, and lazy load dependencies).
13
 */
14
class DoctrineEventProxy
15
{
16
    /**
17
     * @var EntityCollection
18
     */
19
    protected $loaders;
20
21
    /**
22
     * @var EntityCollection
23
     */
24
    protected $unsupportedClasses;
25
26
    /**
27
     * Construct.
28
     */
29
    public function __construct()
30
    {
31
        $this->loaders = new EntityCollection();
32
        $this->unsupportedClasses = new EntityCollection();
33
    }
34
35
    /**
36
     * Register a loader for given entity class.
37
     *
38
     * @param string $entityClass
39
     * @param string $loader
40
     */
41
    public function registerDoctrineLazyLoader($entityClass, LazyLoaderInterface $loader)
42
    {
43
        if (!is_a($entityClass, LazyPropertiesInterface::class, true)) {
44
            throw new \InvalidArgumentException(sprintf(
45
                'Class %s has to implement %s to be able to lazy load her properties.',
46
                $entityClass,
47
                LazyPropertiesInterface::class
48
            ));
49
        }
50
51
        $this->loaders->set($entityClass, $loader);
52
    }
53
54
    /**
55
     * Retrieve a lazy loader for given class (or not).
56
     *
57
     * @param object $entityClass
0 ignored issues
show
Documentation introduced by
There is no parameter named $entityClass. Did you maybe mean $entity?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
58
     *
59
     * @return
60
     */
61
    private function getLoader($entity)
62
    {
63
        $entityClass = get_class($entity);
64
65
        if ($this->unsupportedClasses->containsKey($entityClass)) {
66
            return;
67
        }
68
        if ($this->loaders->containsKey($entityClass)) {
69
            return $this->loaders->get($entityClass);
70
        }
71
        foreach ($this->loaders as $handledClass => $loader) {
72
            if (is_a($entity, $handledClass)) {
73
                $this->loaders->set($entityClass, $loader);
74
75
                return $loader;
76
            }
77
        }
78
79
        $this->unsupportedClasses->set($entityClass, false);
80
    }
81
82
    /**
83
     * "postLoad" Doctrine event handler, notify loaders if define for given event related entity.
84
     *
85
     * @param LifecycleEventArgs $event
86
     */
87
    public function postLoad(LifecycleEventArgs $event)
88
    {
89
        if (!$loader = $this->getLoader($entity = $event->getEntity())) {
90
            return;
91
        }
92
93
        $proxies = $loader->getLoadingDelegates();
94
95
        // global handler
96
        if (isset($proxies[$loaderClass = get_class($loader)])) {
97
            $proxies[$loaderClass]($entity);
98
            unset($proxies[$loaderClass]);
99
        }
100
101
        // define delegates into object
102
        $entity->registerLoaders($proxies);
103
    }
104
}
105