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; |
|
|
|
|
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
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.