|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Antidot\EventSource\Domain\Event; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use JsonSerializable; |
|
10
|
|
|
use Psr\EventDispatcher\StoppableEventInterface; |
|
11
|
|
|
use Ramsey\Uuid\Uuid; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AggregateChanged implements StoppableEventInterface, JsonSerializable |
|
15
|
|
|
{ |
|
16
|
|
|
protected const VERSION = 1; |
|
17
|
|
|
protected const DATE_FORMAT = 'Y-m-d H:i:s'; |
|
18
|
|
|
protected const PROPERTIES = [ |
|
19
|
|
|
'aggregate_id', |
|
20
|
|
|
'payload', |
|
21
|
|
|
'occurred_on', |
|
22
|
|
|
]; |
|
23
|
|
|
protected string $eventId; |
|
24
|
|
|
protected string $aggregateId; |
|
25
|
|
|
protected array $payload; |
|
26
|
|
|
protected int $version; |
|
27
|
|
|
protected bool $stopped = false; |
|
28
|
|
|
protected DateTimeImmutable $occurredOn; |
|
29
|
|
|
|
|
30
|
|
|
final public function __construct( |
|
31
|
|
|
string $eventId, |
|
32
|
|
|
string $aggregateId, |
|
33
|
|
|
array $payload, |
|
34
|
|
|
DateTimeImmutable $occurredOn |
|
35
|
|
|
) { |
|
36
|
|
|
$this->eventId = $eventId; |
|
37
|
|
|
$this->aggregateId = $aggregateId; |
|
38
|
|
|
$this->payload = $payload; |
|
39
|
|
|
$this->occurredOn = $occurredOn; |
|
40
|
|
|
/** @var int $version */ |
|
41
|
|
|
$version = is_int(static::VERSION) ? static::VERSION : 1; |
|
|
|
|
|
|
42
|
|
|
$this->version = $version; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function occur(string $aggregateId, array $payload): self |
|
46
|
|
|
{ |
|
47
|
|
|
$now = DateTimeImmutable::createFromFormat(self::DATE_FORMAT, date(self::DATE_FORMAT)); |
|
48
|
|
|
if (false === $now) { |
|
49
|
|
|
throw new RuntimeException('Error ocurred obtaining datetime from server.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return new static( |
|
53
|
|
|
Uuid::uuid4()->toString(), |
|
54
|
|
|
$aggregateId, |
|
55
|
|
|
$payload, |
|
56
|
|
|
$now |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private static function assertValidSerialisedEvent(array $serializedEvent): void |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
foreach (self::PROPERTIES as $property) { |
|
63
|
|
|
if (array_key_exists($property, $serializedEvent)) { |
|
64
|
|
|
throw new InvalidArgumentException(sprintf( |
|
65
|
|
|
'Property %s is required to reconstitute an event.', |
|
66
|
|
|
$property |
|
67
|
|
|
)); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function eventId(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->eventId; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function aggregateId(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->aggregateId; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function payload(): array |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->payload; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function occurredOn(): DateTimeImmutable |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->occurredOn; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function isPropagationStopped(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->stopped; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function version(): int |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->version; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function jsonSerialize(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
'name' => static::class, |
|
106
|
|
|
'aggregate_id' => $this->aggregateId, |
|
107
|
|
|
'payload' => $this->payload, |
|
108
|
|
|
'occurred_on' => $this->occurredOn, |
|
109
|
|
|
'version' => $this->version, |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|