Completed
Branch 2.1 (d620e0)
by Peter
04:32
created

EventPuller::pullFromEntities()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 4
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Bundle\DomainEvent\Service;
12
13
use Doctrine\Common\Persistence\Proxy;
14
use Doctrine\ORM\EntityManagerInterface;
15
use GpsLab\Domain\Event\Aggregator\AggregateEvents;
16
use GpsLab\Domain\Event\Event;
17
18
class EventPuller
19
{
20
    /**
21
     * @param EntityManagerInterface $em
22
     *
23
     * @return Event[]
24
     */
25 15
    public function pull(EntityManagerInterface $em)
26
    {
27 15
        $uow = $em->getUnitOfWork();
28 15
        $events = [];
29
30 15
        $events = array_merge($events, $this->pullFromEntities($uow->getScheduledEntityDeletions()));
31 15
        $events = array_merge($events, $this->pullFromEntities($uow->getScheduledEntityInsertions()));
32 15
        $events = array_merge($events, $this->pullFromEntities($uow->getScheduledEntityUpdates()));
33
34
        // other entities
35 15
        foreach ($uow->getIdentityMap() as $entities) {
36 8
            $events = array_merge($events, $this->pullFromEntities($entities));
37
        }
38
39 15
        return $events;
40
    }
41
42
    /**
43
     * @param array $entities
44
     *
45
     * @return Event[]
46
     */
47 15
    private function pullFromEntities(array $entities)
48
    {
49 15
        $events = [];
50 15
        foreach ($entities as $entity) {
51
            // ignore Doctrine proxy classes
52
            // proxy class can't have a domain events
53 14
            if (!($entity instanceof Proxy) && $entity instanceof AggregateEvents) {
54 14
                $events = array_merge($events, $entity->pullEvents());
55
            }
56
        }
57
58 15
        return $events;
59
    }
60
}
61