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.
Test Failed
Pull Request — master (#7)
by Fábio Tadeu da
01:42
created

EventsHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 75.76%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 83
rs 10
c 0
b 0
f 0
ccs 25
cts 33
cp 0.7576
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A persist() 0 10 2
A convert() 0 25 5
A getEventAwareEntities() 0 10 1
A getDomainsEvents() 0 16 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 class_exists;
16
use function get_class;
17
use function ksort;
18
19
abstract class EventsHandler
20
{
21
    /**
22
     * @return DomainEvent[]
23
     */
24 1
    protected function getDomainsEvents(OnFlushEventArgs $eventArgs) : array
25
    {
26 1
        $unitOfWork = $eventArgs->getEntityManager()
27 1
            ->getUnitOfWork();
28
29 1
        $events = [];
30
31 1
        foreach (self::getEventAwareEntities($unitOfWork) as $entity) {
32 1
            $events += $entity->expelRecordedEvents();
33
        }
34
35 1
        ksort($events);
36
37 1
        Counter::reset();
38
39 1
        return $events;
40
    }
41
42
    /**
43
     * @param string[]      $conversionMap
44
     * @param DomainEvent[] $domainEvents
45
     *
46
     * @return array
47
     */
48 1
    protected function convert(array $conversionMap, DomainEvent ...$domainEvents) : array
49
    {
50 1
        $outboxEntries = [];
51
52 1
        foreach ($domainEvents as $domainEvent) {
53 1
            $domainEventClassName = get_class($domainEvent);
54
55 1
            if (! isset($conversionMap[$domainEventClassName])) {
56
                continue;
57
            }
58
59 1
            if (! class_exists($conversionMap[$domainEventClassName])) {
60
                continue;
61
            }
62
63 1
            $outboxEvent = new $conversionMap[$domainEventClassName]($domainEvent);
64
65 1
            if (! $outboxEvent instanceof OutboxEntry) {
66
                continue;
67
            }
68
69 1
            $outboxEntries[] = $outboxEvent;
70
        }
71
72 1
        return $outboxEntries;
73
    }
74
75
    protected function persist(
76
        EntityManagerInterface $entityManager,
77
        OutboxMappedSuperclass $outboxMappedSuperclass,
78
        OutboxEntry ...$outboxEntries
79
    ) : void {
80
        foreach ($outboxEntries as $outboxEntry) {
81
            $entityManager->persist($outboxMappedSuperclass->withOutboxEntry($outboxEntry));
82
        }
83
84
        $entityManager->getUnitOfWork()->computeChangeSets();
85
    }
86
87
    abstract function onFlush(OnFlushEventArgs $eventArgs) : void;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
89
    /**
90
     * @return EventAware[]
91
     */
92 1
    private static function getEventAwareEntities(UnitOfWork $unitOfWork) : array
93
    {
94 1
        $entities = array_merge(
95 1
            $unitOfWork->getScheduledEntityInsertions(),
96 1
            $unitOfWork->getScheduledEntityUpdates(),
97 1
            $unitOfWork->getScheduledEntityDeletions()
98
        );
99
100
        return array_filter($entities, static function ($entity) {
101 1
            return $entity instanceof EventAware;
102 1
        });
103
    }
104
}
105