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); |
|
|
|
|
42
|
|
|
} |
43
|
2 |
|
|
44
|
2 |
|
/** |
45
|
|
|
* @return DomainEvent[] |
46
|
|
|
*/ |
47
|
|
|
protected function getDomainEvents(OnFlushEventArgs $eventArgs): array |
48
|
|
|
{ |
49
|
3 |
|
$unitOfWork = $eventArgs->getEntityManager() |
|
|
|
|
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
|
|
|
|
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.