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

EventsHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 25
dl 0
loc 74
rs 10
c 0
b 0
f 0
ccs 31
cts 31
cp 1

5 Methods

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