|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Dsantang\DomainEventsDoctrine\Outbox; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
9
|
|
|
use Doctrine\ORM\Mapping\MappedSuperclass; |
|
10
|
|
|
use Ramsey\Uuid\Uuid; |
|
11
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @MappedSuperclass |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class OutboxMappedSuperclass |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @ORM\Id |
|
20
|
|
|
* @ORM\Column(type="uuid") |
|
21
|
|
|
*/ |
|
22
|
|
|
protected UuidInterface $id; |
|
23
|
|
|
|
|
24
|
|
|
/** @ORM\Column(type="string") */ |
|
25
|
|
|
protected string $messageKey; |
|
26
|
|
|
|
|
27
|
|
|
/** @ORM\Column(type="string") */ |
|
28
|
|
|
protected string $messageRoute; |
|
29
|
|
|
|
|
30
|
|
|
/** @ORM\Column(type="string") */ |
|
31
|
|
|
protected string $messageType; |
|
32
|
|
|
|
|
33
|
|
|
/** @ORM\Column(type="uuid") */ |
|
34
|
|
|
protected UuidInterface $aggregateId; |
|
35
|
|
|
|
|
36
|
|
|
/** @ORM\Column(type="string") */ |
|
37
|
|
|
protected string $aggregateType; |
|
38
|
|
|
|
|
39
|
|
|
/** @ORM\Column(type="string") */ |
|
40
|
|
|
protected string $payloadType; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @ORM\Column(type="json", options={"jsonb"=true}) |
|
44
|
|
|
* |
|
45
|
|
|
* @var mixed[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected array $payload; |
|
48
|
|
|
|
|
49
|
|
|
/** @ORM\Column(type="integer") */ |
|
50
|
|
|
protected int $schemaVersion; |
|
51
|
|
|
|
|
52
|
|
|
/** @ORM\Column(type="utc_datetime_immutable") */ |
|
53
|
|
|
protected DateTimeImmutable $createdAt; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @ORM\Column(type="uuid", nullable=true) |
|
57
|
|
|
* @ORM\OneToOne(targetEntity=OutboxMappedSuperclass::class) |
|
58
|
|
|
*/ |
|
59
|
|
|
protected ?UuidInterface $previousEvent = null; |
|
60
|
|
|
|
|
61
|
|
|
public function fromOutboxEntry( |
|
62
|
|
|
OutboxEntry $outboxEntry, |
|
63
|
|
|
?OutboxMappedSuperclass $previousEntity = null |
|
64
|
|
|
): OutboxMappedSuperclass { |
|
65
|
|
|
$outbox = clone $this; |
|
66
|
|
|
|
|
67
|
|
|
$outbox->id = Uuid::uuid4(); |
|
68
|
|
|
$outbox->messageKey = $outboxEntry->getMessageKey(); |
|
69
|
|
|
$outbox->messageRoute = $outboxEntry->getMessageRoute(); |
|
70
|
|
|
$outbox->messageType = $outboxEntry->getMessageType(); |
|
71
|
|
|
$outbox->aggregateId = $outboxEntry->getAggregateId(); |
|
72
|
|
|
$outbox->aggregateType = $outboxEntry->getAggregateType(); |
|
73
|
|
|
$outbox->payloadType = $outboxEntry->getPayloadType(); |
|
74
|
|
|
$outbox->payload = $outboxEntry->getPayload(); |
|
75
|
|
|
$outbox->schemaVersion = $outboxEntry->getSchemaVersion(); |
|
76
|
|
|
$outbox->createdAt = new DateTimeImmutable(); |
|
77
|
|
|
|
|
78
|
|
|
if ($previousEntity instanceof OutboxMappedSuperclass) { |
|
79
|
|
|
$outbox->previousEvent = $previousEntity->getId(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $outbox; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function getId(): UuidInterface |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->id; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|