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\Store; |
14
|
|
|
|
15
|
|
|
use function ServiceBus\Common\datetimeInstantiator; |
16
|
|
|
use function ServiceBus\Common\datetimeToString; |
17
|
|
|
use ServiceBus\EventSourcing\AggregateId; |
18
|
|
|
use ServiceBus\EventSourcing\EventStream\AggregateEvent; |
19
|
|
|
use ServiceBus\EventSourcing\EventStream\AggregateEventStream; |
20
|
|
|
use ServiceBus\EventSourcing\EventStream\Serializer\EventSerializer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
* |
25
|
|
|
* @throws \ServiceBus\Common\Exceptions\DateTimeException |
26
|
|
|
* @throws \ServiceBus\EventSourcing\EventStream\Serializer\Exceptions\SerializeEventFailed |
27
|
|
|
*/ |
28
|
|
|
function streamToDomainRepresentation(EventSerializer $serializer, StoredAggregateEventStream $storedAggregateEventsStream): AggregateEventStream |
29
|
|
|
{ |
30
|
5 |
|
$events = []; |
31
|
|
|
|
32
|
5 |
|
foreach($storedAggregateEventsStream->storedAggregateEvents as $storedAggregateEvent) |
33
|
|
|
{ |
34
|
4 |
|
$events[] = eventToDomainRepresentation($serializer, $storedAggregateEvent); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @var \DateTimeImmutable $createdAt */ |
38
|
1 |
|
$createdAt = datetimeInstantiator($storedAggregateEventsStream->createdAt); |
39
|
|
|
/** @var \DateTimeImmutable|null $closedAt */ |
40
|
1 |
|
$closedAt = datetimeInstantiator($storedAggregateEventsStream->closedAt); |
41
|
|
|
|
42
|
|
|
/** @psalm-var class-string<\ServiceBus\EventSourcing\AggregateId> $idClass */ |
43
|
1 |
|
$idClass = $storedAggregateEventsStream->aggregateIdClass; |
44
|
|
|
|
45
|
|
|
/** @var AggregateId $id */ |
46
|
1 |
|
$id = new $idClass($storedAggregateEventsStream->aggregateId); |
47
|
|
|
|
48
|
1 |
|
return new AggregateEventStream( |
49
|
1 |
|
$id, |
50
|
1 |
|
$storedAggregateEventsStream->aggregateClass, |
51
|
|
|
$events, |
52
|
|
|
$createdAt, |
53
|
|
|
$closedAt |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @internal |
59
|
|
|
* |
60
|
|
|
* @throws \ServiceBus\Common\Exceptions\DateTimeException |
61
|
|
|
* @throws \ServiceBus\EventSourcing\EventStream\Serializer\Exceptions\SerializeEventFailed |
62
|
|
|
*/ |
63
|
|
|
function eventToStoredRepresentation(EventSerializer $serializer, AggregateEvent $aggregateEvent): StoredAggregateEvent |
64
|
|
|
{ |
65
|
|
|
/** @psalm-var class-string $eventClass */ |
66
|
7 |
|
$eventClass = \get_class($aggregateEvent->event); |
67
|
|
|
|
68
|
7 |
|
return StoredAggregateEvent::create( |
69
|
7 |
|
$aggregateEvent->id, |
70
|
7 |
|
$aggregateEvent->playhead, |
71
|
7 |
|
$serializer->serialize($aggregateEvent->event), |
72
|
|
|
$eventClass, |
73
|
7 |
|
(string) datetimeToString($aggregateEvent->occuredAt) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @internal |
79
|
|
|
* |
80
|
|
|
* @throws \ServiceBus\Common\Exceptions\DateTimeException |
81
|
|
|
* @throws \ServiceBus\EventSourcing\EventStream\Serializer\Exceptions\SerializeEventFailed |
82
|
|
|
*/ |
83
|
|
|
function eventToDomainRepresentation(EventSerializer $serializer, StoredAggregateEvent $storedAggregateEvent): AggregateEvent |
84
|
|
|
{ |
85
|
|
|
/** @var \DateTimeImmutable $occuredAt */ |
86
|
4 |
|
$occuredAt = datetimeInstantiator($storedAggregateEvent->occuredAt); |
87
|
|
|
|
88
|
|
|
/** @var \DateTimeImmutable $recordedAt */ |
89
|
4 |
|
$recordedAt = datetimeInstantiator($storedAggregateEvent->recordedAt); |
90
|
|
|
|
91
|
4 |
|
return AggregateEvent::restore( |
92
|
4 |
|
$storedAggregateEvent->eventId, |
93
|
4 |
|
$serializer->unserialize( |
94
|
4 |
|
$storedAggregateEvent->eventClass, |
95
|
4 |
|
$storedAggregateEvent->eventData |
96
|
|
|
), |
97
|
|
|
$storedAggregateEvent->playheadPosition, |
98
|
|
|
$occuredAt, |
99
|
|
|
$recordedAt |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws \ServiceBus\Common\Exceptions\DateTimeException |
105
|
|
|
*/ |
106
|
|
|
function streamToStoredRepresentation( |
107
|
|
|
EventSerializer $serializer, |
108
|
|
|
AggregateEventStream $aggregateEvent |
109
|
|
|
): StoredAggregateEventStream |
110
|
|
|
{ |
111
|
7 |
|
$preparedEvents = \array_map( |
112
|
|
|
static function(AggregateEvent $aggregateEvent) use ($serializer): StoredAggregateEvent |
113
|
|
|
{ |
114
|
7 |
|
return eventToStoredRepresentation($serializer, $aggregateEvent); |
115
|
7 |
|
}, |
116
|
7 |
|
$aggregateEvent->events |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @psalm-var class-string<\ServiceBus\EventSourcing\AggregateId> $eventClass |
121
|
|
|
* @psalm-var array<int, \ServiceBus\EventSourcing\EventStream\Store\StoredAggregateEvent> $preparedEvents |
122
|
|
|
*/ |
123
|
7 |
|
$eventClass = \get_class($aggregateEvent->id); |
124
|
|
|
|
125
|
7 |
|
return new StoredAggregateEventStream( |
126
|
7 |
|
$aggregateEvent->id->toString(), |
127
|
|
|
$eventClass, |
128
|
7 |
|
$aggregateEvent->aggregateClass, |
129
|
|
|
$preparedEvents, |
130
|
7 |
|
(string) datetimeToString($aggregateEvent->createdAt) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|