Failed Conditions
Pull Request — master (#6392)
by Alessandro
11:37
created

HydrationCompleteHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Internal;
21
22
use Doctrine\ORM\EntityManagerInterface;
23
use Doctrine\ORM\Event\LifecycleEventArgs;
24
use Doctrine\ORM\Event\ListenersInvoker;
25
use Doctrine\ORM\Events;
26
use Doctrine\ORM\Mapping\ClassMetadata;
27
28
/**
29
 * Class, which can handle completion of hydration cycle and produce some of tasks.
30
 * In current implementation triggers deferred postLoad event.
31
 *
32
 * @author Artur Eshenbrener <[email protected]>
33
 * @since 2.5
34
 */
35
final class HydrationCompleteHandler
36
{
37
    /**
38
     * @var ListenersInvoker
39
     */
40
    private $listenersInvoker;
41
42
    /**
43
     * @var EntityManagerInterface
44
     */
45
    private $em;
46
47
    /**
48
     * @var array[]
49
     */
50
    private $deferredPostLoadInvocations = [];
51
52
    /**
53
     * Constructor for this object
54
     *
55
     * @param ListenersInvoker $listenersInvoker
56
     * @param EntityManagerInterface $em
57
     */
58 2412
    public function __construct(ListenersInvoker $listenersInvoker, EntityManagerInterface $em)
59
    {
60 2412
        $this->listenersInvoker = $listenersInvoker;
61 2412
        $this->em               = $em;
62 2412
    }
63
64
    /**
65
     * Method schedules invoking of postLoad entity to the very end of current hydration cycle.
66
     *
67
     * @param ClassMetadata $class
68
     * @param object        $entity
69
     */
70 713
    public function deferPostLoadInvoking(ClassMetadata $class, $entity)
71
    {
72 713
        $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postLoad);
73
74 713
        if ($invoke === ListenersInvoker::INVOKE_NONE) {
75 664
            return;
76
        }
77
78 99
        $this->deferredPostLoadInvocations[] = [$class, $invoke, $entity];
79 99
    }
80
81
    /**
82
     * This method should me called after any hydration cycle completed.
83
     *
84
     * Method fires all deferred invocations of postLoad events
85
     */
86 933
    public function hydrationComplete()
87
    {
88 933
        $toInvoke                          = $this->deferredPostLoadInvocations;
89 933
        $this->deferredPostLoadInvocations = [];
90
91 933
        foreach ($toInvoke as $classAndEntity) {
92 98
            list($class, $invoke, $entity) = $classAndEntity;
93
94 98
            $this->listenersInvoker->invoke(
95 98
                $class,
96 98
                Events::postLoad,
97 98
                $entity,
98 98
                new LifecycleEventArgs($entity, $this->em),
99 98
                $invoke
100
            );
101
        }
102 933
    }
103
}
104