1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Event Sourcing implementation. |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\EventSourcing\EventStream; |
14
|
|
|
|
15
|
|
|
use ServiceBus\EventSourcing\AggregateId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Event stream. |
19
|
|
|
* |
20
|
|
|
* @psalm-readonly |
21
|
|
|
*/ |
22
|
|
|
final class AggregateEventStream |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Stream (aggregate) identifier. |
26
|
|
|
*/ |
27
|
|
|
public AggregateId $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Aggregate class. |
31
|
|
|
* |
32
|
|
|
* @psalm-var class-string<\ServiceBus\EventSourcing\Aggregate> |
33
|
|
|
*/ |
34
|
|
|
public string $aggregateClass; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Event collection. |
38
|
|
|
* |
39
|
|
|
* @psalm-var array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> |
40
|
|
|
* |
41
|
|
|
* @var \ServiceBus\EventSourcing\EventStream\AggregateEvent[] |
42
|
|
|
*/ |
43
|
|
|
public array $events; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Origin event collection. |
47
|
|
|
* |
48
|
|
|
* @psalm-var array<int, object> |
49
|
|
|
* |
50
|
|
|
* @var object[] |
51
|
|
|
*/ |
52
|
|
|
public array $originEvents; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Created at datetime. |
56
|
|
|
*/ |
57
|
|
|
public \DateTimeImmutable $createdAt; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Closed at datetime. |
61
|
|
|
*/ |
62
|
|
|
public ?\DateTimeImmutable $closedAt = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @psalm-param class-string<\ServiceBus\EventSourcing\Aggregate> $aggregateClass |
66
|
|
|
* @psalm-param array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> $events |
67
|
|
|
* |
68
|
|
|
* @param \ServiceBus\EventSourcing\EventStream\AggregateEvent[] $events |
69
|
|
|
*/ |
70
|
9 |
|
public function __construct( |
71
|
|
|
AggregateId $id, |
72
|
|
|
string $aggregateClass, |
73
|
|
|
array $events, |
74
|
|
|
\DateTimeImmutable $createdAt, |
75
|
|
|
?\DateTimeImmutable $closedAt |
76
|
|
|
) |
77
|
|
|
{ |
78
|
9 |
|
$this->id = $id; |
79
|
9 |
|
$this->aggregateClass = $aggregateClass; |
80
|
9 |
|
$this->events = self::sortEvents($events); |
81
|
9 |
|
$this->originEvents = self::extractOriginEvents($this->events); |
82
|
9 |
|
$this->createdAt = $createdAt; |
83
|
9 |
|
$this->closedAt = $closedAt; |
84
|
9 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @psalm-param array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> $events |
88
|
|
|
* |
89
|
|
|
* @psalm-return array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> |
90
|
|
|
*/ |
91
|
9 |
|
private static function sortEvents(array $events): array |
92
|
|
|
{ |
93
|
9 |
|
$result = []; |
94
|
|
|
|
95
|
9 |
|
foreach($events as $aggregateEvent) |
96
|
|
|
{ |
97
|
|
|
/** @var \ServiceBus\EventSourcing\EventStream\AggregateEvent $aggregateEvent */ |
98
|
9 |
|
$result[$aggregateEvent->playhead] = $aggregateEvent; |
99
|
|
|
} |
100
|
|
|
|
101
|
9 |
|
\ksort($result); |
102
|
|
|
|
103
|
9 |
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @psalm-param array<int, \ServiceBus\EventSourcing\EventStream\AggregateEvent> $events |
108
|
|
|
* @psalm-return array<int, object> |
109
|
|
|
* |
110
|
|
|
* @param \ServiceBus\EventSourcing\EventStream\AggregateEvent[] $events |
111
|
|
|
* |
112
|
|
|
* @return object[] |
113
|
|
|
*/ |
114
|
9 |
|
private static function extractOriginEvents(array $events): array |
115
|
|
|
{ |
116
|
9 |
|
return \array_map( |
117
|
|
|
static function(AggregateEvent $event): object |
118
|
|
|
{ |
119
|
9 |
|
return $event->event; |
120
|
9 |
|
}, |
121
|
9 |
|
$events |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|