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.

EventsHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 31
c 1
b 0
f 0
dl 0
loc 88
rs 10
ccs 37
cts 37
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getDomainEvents() 0 20 3
A persist() 0 16 3
A onFlush() 0 7 1
A getDeletionAwareEntities() 0 5 1
A getEventAwareEntities() 0 9 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
15
use function array_filter;
16
use function array_merge;
17
use function array_push;
18
use function count;
19
use function ksort;
20
21
abstract class EventsHandler
22
{
23
    protected OutboxMappedSuperclass $outboxMappedSuperclass;
24
25 10
    public function __construct(OutboxMappedSuperclass $outboxMappedSuperclass)
26
    {
27 10
        $this->outboxMappedSuperclass = $outboxMappedSuperclass;
28 10
    }
29
30
    /**
31
     * @return OutboxEntry[]
32
     */
33
    abstract protected function convert(DomainEvent ...$domainEvents): array;
34
35
    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);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\Event\OnFlu...rgs::getEntityManager() has been deprecated: 2.13. Use {@see getObjectManager} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
        $this->persist(/** @scrutinizer ignore-deprecated */ $eventArgs->getEntityManager(), ...$outboxEvents);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
    }
43 2
44 2
    /**
45
     * @return DomainEvent[]
46
     */
47
    protected function getDomainEvents(OnFlushEventArgs $eventArgs): array
48
    {
49 3
        $unitOfWork = $eventArgs->getEntityManager()
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\Event\OnFlu...rgs::getEntityManager() has been deprecated: 2.13. Use {@see getObjectManager} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
        $unitOfWork = /** @scrutinizer ignore-deprecated */ $eventArgs->getEntityManager()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
50
                                ->getUnitOfWork();
51 3
52 3
        $events = [];
53
54 3
        foreach (self::getEventAwareEntities($unitOfWork) as $entity) {
55
            $events += $entity->expelRecordedEvents();
56 3
        }
57 2
58
        foreach (self::getDeletionAwareEntities($unitOfWork) as $entity) {
59
            array_push($events, $entity->expelDeletionEvents());
60 3
        }
61 2
62
        ksort($events);
63
64 3
        Counter::reset();
65
66 3
        return $events;
67
    }
68 3
69
    protected function persist(EntityManagerInterface $entityManager, OutboxEntry ...$outboxEntries): void
70
    {
71 5
        if (count($outboxEntries) <= 0) {
72
            return;
73 5
        }
74 2
75
        $previousEntity = null;
76
77 3
        foreach ($outboxEntries as $outboxEntry) {
78
            $entity = $this->outboxMappedSuperclass->fromOutboxEntry($outboxEntry, $previousEntity);
79 3
            $entityManager->persist($entity);
80 3
81 3
            $previousEntity = $entity;
82
        }
83 3
84
        $entityManager->getUnitOfWork()->computeChangeSets();
85
    }
86 3
87 3
    /**
88
     * @return EventAware[]
89
     */
90
    private static function getEventAwareEntities(UnitOfWork $unitOfWork): array
91
    {
92 3
        $entities = array_merge(
93
            $unitOfWork->getScheduledEntityInsertions(),
94 3
            $unitOfWork->getScheduledEntityUpdates(),
95 3
            $unitOfWork->getScheduledEntityDeletions()
96 3
        );
97 3
98
        return array_filter($entities, static fn ($entity): bool => $entity instanceof EventAware);
99
    }
100
101 2
    /**
102 3
     * @return DeletionAware[]
103
     */
104
    private static function getDeletionAwareEntities(UnitOfWork $unitOfWork): array
105
    {
106
        return array_filter(
107
            $unitOfWork->getScheduledEntityDeletions(),
108 3
            static fn ($entity): bool => $entity instanceof DeletionAware
109
        );
110
    }
111
}
112