GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 7859c5...12a294 )
by Giacomo
55s queued 12s
created

EventsHandler::getDeletionAwareEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dsantang\DomainEventsDoctrine\Outbox;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Event\OnFlushEventArgs;
9
use Doctrine\ORM\UnitOfWork;
10
use Dsantang\DomainEvents\Counter;
11
use Dsantang\DomainEvents\DeletionAware;
12
use Dsantang\DomainEvents\DomainEvent;
13
use Dsantang\DomainEvents\EventAware;
14
use function array_filter;
15
use function array_merge;
16
use function array_push;
17
use function count;
18
use function ksort;
19
20
abstract class EventsHandler
21
{
22
    /** @var OutboxMappedSuperclass */
23
    protected $outboxMappedSuperclass;
24
25 10
    public function __construct(OutboxMappedSuperclass $outboxMappedSuperclass)
26
    {
27 10
        $this->outboxMappedSuperclass = $outboxMappedSuperclass;
28 10
    }
29
30
    /**
31
     * @return OutboxEntry[]
32
     *
33
     * @var DomainEvent[] $domainEvents
34
     */
35
    abstract protected function convert(DomainEvent ...$domainEvents) : array;
36
37 2
    public function onFlush(OnFlushEventArgs $eventArgs) : void
38
    {
39 2
        $domainEvents = $this->getDomainEvents($eventArgs);
40
41 2
        $outboxEvents = $this->convert(...$domainEvents);
42
43 2
        $this->persist($eventArgs->getEntityManager(), ...$outboxEvents);
44 2
    }
45
46
    /**
47
     * @return DomainEvent[]
48
     */
49 3
    protected function getDomainEvents(OnFlushEventArgs $eventArgs) : array
50
    {
51 3
        $unitOfWork = $eventArgs->getEntityManager()
52 3
                                ->getUnitOfWork();
53
54 3
        $events = [];
55
56 3
        foreach (self::getEventAwareEntities($unitOfWork) as $entity) {
57 2
            $events += $entity->expelRecordedEvents();
58
        }
59
60 3
        foreach (self::getDeletionAwareEntities($unitOfWork) as $entity) {
61 2
            array_push($events, $entity->expelDeletionEvents());
62
        }
63
64 3
        ksort($events);
65
66 3
        Counter::reset();
67
68 3
        return $events;
69
    }
70
71 5
    protected function persist(EntityManagerInterface $entityManager, OutboxEntry ...$outboxEntries) : void
72
    {
73 5
        if (count($outboxEntries) <= 0) {
74 2
            return;
75
        }
76
77 3
        $previousEntity = null;
78
79 3
        foreach ($outboxEntries as $outboxEntry) {
80 3
            $entity = $this->outboxMappedSuperclass->fromOutboxEntry($outboxEntry, $previousEntity);
81 3
            $entityManager->persist($entity);
82
83 3
            $previousEntity = $entity;
84
        }
85
86 3
        $entityManager->getUnitOfWork()->computeChangeSets();
87 3
    }
88
89
    /**
90
     * @return EventAware[]
91
     */
92 3
    private static function getEventAwareEntities(UnitOfWork $unitOfWork) : array
93
    {
94 3
        $entities = array_merge(
95 3
            $unitOfWork->getScheduledEntityInsertions(),
96 3
            $unitOfWork->getScheduledEntityUpdates(),
97 3
            $unitOfWork->getScheduledEntityDeletions()
98
        );
99
100
        return array_filter($entities, static function ($entity) {
101 2
            return $entity instanceof EventAware;
102 3
        });
103
    }
104
105
    /**
106
     * @return DeletionAware[]
107
     */
108 3
    private static function getDeletionAwareEntities(UnitOfWork $unitOfWork) : array
109
    {
110
        return array_filter($unitOfWork->getScheduledEntityDeletions(), static function ($entity) {
111 2
            return $entity instanceof DeletionAware;
112 3
        });
113
    }
114
}
115