Completed
Push — master ( 3214c9...a8a248 )
by Quentin
10:44 queued 01:22
created

DoctrineEventProxy::getLoader()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 8.8571
cc 5
eloc 11
nc 5
nop 1
crap 30
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
        // global handler (deprecated)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94 View Code Duplication
        if (isset($proxies[$loaderClass = static::class])) {
0 ignored issues
show
Bug introduced by
The variable $proxies seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
96
                sprintf('Global loader delegate is deprecated and will be removed in 2.0. Make "%s" loader invokable instead, entity to handle is given at first parameter.',
97
                    static::class
98
                ),
99
                E_USER_DEPRECATED
100
            );
101
            $proxies[$loaderClass]($entity);
102
            unset($proxies[$loaderClass]);
103
        }
104
105
        // define delegates into object
106
        $entity->registerLoaders(
107
            $loader->getLoadingDelegates()
108
        );
109
110
        // global delegate if able to
111
        if (is_callable($loader)) {
112
            $loader($entity);
113
        }
114
    }
115
}
116