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