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