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
Pull Request — master (#7)
by Fábio Tadeu da
03:01 queued 55s
created

EventsHandler::onFlush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 1
crap 2
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\DomainEvent;
12
use Dsantang\DomainEvents\EventAware;
13
use function array_filter;
14
use function array_merge;
15
use function ksort;
16
17
abstract class EventsHandler
18
{
19
    /** @var OutboxMappedSuperclass */
20
    protected $outboxMappedSuperclass;
21
22
    /**
23
     * @return DomainEvent[]
24
     */
25 4
    protected function getDomainEvents(OnFlushEventArgs $eventArgs) : array
26
    {
27 4
        $unitOfWork = $eventArgs->getEntityManager()
28 4
            ->getUnitOfWork();
29
30 4
        $events = [];
31
32 4
        foreach (self::getEventAwareEntities($unitOfWork) as $entity) {
33 3
            $events += $entity->expelRecordedEvents();
34
        }
35
36 4
        ksort($events);
37
38 4
        Counter::reset();
39
40 4
        return $events;
41
    }
42
43
    /**
44
     * @param DomainEvent[] $domainEvents
45
     *
46
     * @return OutboxEntry[]
47
     */
48
    abstract protected function convert(DomainEvent ...$domainEvents) : array;
49
50 2
    public function onFlush(OnFlushEventArgs $eventArgs) : void
51
    {
52 2
        $domainEvents = $this->getDomainEvents($eventArgs);
53
54 2
        $outboxEvents = $this->convert(...$domainEvents);
55
56 2
        if (empty($outboxEvents)) {
57 1
            return;
58
        }
59
60 1
        $this->persist($eventArgs->getEntityManager(), ...$outboxEvents);
61 1
    }
62
63 2
    protected function persist(EntityManagerInterface $entityManager, OutboxEntry ...$outboxEntries) : void
64
    {
65 2
        foreach ($outboxEntries as $outboxEntry) {
66 2
            $entity = $this->outboxMappedSuperclass->fromOutboxEntry($outboxEntry);
67 2
            $entityManager->persist($entity);
68
        }
69
70 2
        $entityManager->getUnitOfWork()->computeChangeSets();
71 2
    }
72
73
    /**
74
     * @return EventAware[]
75
     */
76 4
    private static function getEventAwareEntities(UnitOfWork $unitOfWork) : array
77
    {
78 4
        $entities = array_merge(
79 4
            $unitOfWork->getScheduledEntityInsertions(),
80 4
            $unitOfWork->getScheduledEntityUpdates(),
81 4
            $unitOfWork->getScheduledEntityDeletions()
82
        );
83
84
        return array_filter($entities, static function ($entity) {
85 3
            return $entity instanceof EventAware;
86 4
        });
87
    }
88
}
89