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
02:54
created

EventsHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 24
dl 0
loc 69
rs 10
c 0
b 0
f 0
ccs 28
cts 28
cp 1

4 Methods

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