-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathInMemoryStore.php
More file actions
317 lines (268 loc) · 11 KB
/
Copy pathInMemoryStore.php
File metadata and controls
317 lines (268 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Store;
use Closure;
use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\Message\HeaderNotFound;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Store\Criteria\AggregateIdCriterion;
use Patchlevel\EventSourcing\Store\Criteria\AggregateNameCriterion;
use Patchlevel\EventSourcing\Store\Criteria\ArchivedCriterion;
use Patchlevel\EventSourcing\Store\Criteria\Criteria;
use Patchlevel\EventSourcing\Store\Criteria\EventsCriterion;
use Patchlevel\EventSourcing\Store\Criteria\FromIndexCriterion;
use Patchlevel\EventSourcing\Store\Criteria\FromPlayheadCriterion;
use Patchlevel\EventSourcing\Store\Criteria\StreamCriterion;
use Patchlevel\EventSourcing\Store\Criteria\ToIndexCriterion;
use Patchlevel\EventSourcing\Store\Header\EventIdHeader;
use Patchlevel\EventSourcing\Store\Header\IndexHeader;
use Patchlevel\EventSourcing\Store\Header\PlayheadHeader;
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
use Patchlevel\EventSourcing\Store\Header\StreamNameHeader;
use Psr\Clock\ClockInterface;
use Ramsey\Uuid\Uuid;
use Throwable;
use function array_filter;
use function array_map;
use function array_reverse;
use function array_slice;
use function array_unique;
use function array_values;
use function count;
use function in_array;
use function mb_substr;
use function str_ends_with;
use function str_starts_with;
use const ARRAY_FILTER_USE_BOTH;
final class InMemoryStore implements StreamStore
{
/** @var array<0|positive-int, Message> */
private array $messages = [];
/** @param list<Message> $messages */
public function __construct(
array $messages = [],
private readonly EventRegistry|null $eventRegistry = null,
private readonly ClockInterface $clock = new SystemClock(),
) {
$this->save(...$messages);
}
public function load(
Criteria|null $criteria = null,
int|null $limit = null,
int|null $offset = null,
bool $backwards = false,
): ArrayStream {
$messages = $this->filter($criteria);
if ($backwards) {
$messages = array_reverse($messages);
}
if ($offset !== null) {
$messages = array_slice($messages, $offset);
}
if ($limit !== null) {
$messages = array_slice($messages, 0, $limit);
}
return new ArrayStream($messages);
}
public function count(Criteria|null $criteria = null): int
{
return count($this->filter($criteria));
}
public function save(Message ...$messages): void
{
$this->transactional(function () use ($messages): void {
$count = count($this->messages);
foreach ($messages as $message) {
$count++;
if (!$message->hasHeader(IndexHeader::class)) {
$message = $message->withHeader(new IndexHeader($count));
}
if (!$message->hasHeader(EventIdHeader::class)) {
$message = $message->withHeader(new EventIdHeader(Uuid::uuid7()->toString()));
}
if (!$message->hasHeader(RecordedOnHeader::class)) {
$message = $message->withHeader(new RecordedOnHeader($this->clock->now()));
}
$this->messages[] = $message;
}
});
}
/**
* @param Closure():ClosureReturn $function
*
* @template ClosureReturn
*/
public function transactional(Closure $function): void
{
$messages = $this->messages;
try {
$function();
} catch (Throwable $e) {
$this->messages = $messages;
throw $e;
}
}
/** @return list<string> */
public function streams(): array
{
return array_values(
array_unique(
array_filter(
array_map(
static function (Message $message): string|null {
try {
return $message->header(AggregateHeader::class)->streamName();
} catch (HeaderNotFound) {
try {
return $message->header(StreamNameHeader::class)->streamName;
} catch (HeaderNotFound) {
return null;
}
}
},
$this->messages,
),
static fn (string|null $streamName): bool => $streamName !== null,
),
),
);
}
public function remove(Criteria|null $criteria = null): void
{
$messagesToRemove = $this->filter($criteria);
$this->messages = array_filter(
$this->messages,
static fn (Message $message): bool => !in_array($message, $messagesToRemove, true),
);
}
public function archive(Criteria|null $criteria = null): void
{
foreach ($this->filter($criteria) as $key => $message) {
$this->messages[$key] = $message->withHeader(new ArchivedHeader());
}
}
/** @return array<positive-int|0, Message> */
private function filter(Criteria|null $criteria): array
{
if (!$criteria) {
return $this->messages;
}
$eventRegistry = $this->eventRegistry;
return array_filter(
$this->messages,
static function (Message $message) use ($criteria, $eventRegistry): bool {
foreach ($criteria->all() as $criterion) {
switch ($criterion::class) {
case AggregateIdCriterion::class:
try {
if ($message->header(AggregateHeader::class)->aggregateId !== $criterion->aggregateId) {
return false;
}
} catch (HeaderNotFound) {
return false;
}
break;
case AggregateNameCriterion::class:
try {
if ($message->header(AggregateHeader::class)->aggregateName !== $criterion->aggregateName) {
return false;
}
} catch (HeaderNotFound) {
return false;
}
break;
case StreamCriterion::class:
if ($criterion->all()) {
break;
}
try {
$messageStreamName = $message->header(AggregateHeader::class)->streamName();
} catch (HeaderNotFound) {
try {
$messageStreamName = $message->header(StreamNameHeader::class)->streamName;
} catch (HeaderNotFound) {
return false;
}
}
$match = false;
foreach ($criterion->streamName as $streamName) {
if (str_ends_with($streamName, '*')) {
if (str_starts_with($messageStreamName, mb_substr($streamName, 0, -1))) {
$match = true;
break;
}
} else {
if ($streamName === $messageStreamName) {
$match = true;
break;
}
}
}
if (!$match) {
return false;
}
break;
case FromPlayheadCriterion::class:
$playhead = null;
try {
$playhead = $message->header(AggregateHeader::class)->playhead;
} catch (HeaderNotFound) {
try {
$playhead = $message->header(PlayheadHeader::class)->playhead;
} catch (HeaderNotFound) {
return false;
}
}
if ($playhead < $criterion->fromPlayhead) {
return false;
}
break;
case ArchivedCriterion::class:
if (!$message->hasHeader(ArchivedHeader::class) === $criterion->archived) {
return false;
}
break;
case FromIndexCriterion::class:
try {
$index = $message->header(IndexHeader::class)->index;
} catch (HeaderNotFound) {
return false;
}
if ($index <= $criterion->fromIndex) {
return false;
}
break;
case ToIndexCriterion::class:
try {
$index = $message->header(IndexHeader::class)->index;
} catch (HeaderNotFound) {
return false;
}
if ($index >= $criterion->toIndex) {
return false;
}
break;
case EventsCriterion::class:
if ($eventRegistry === null) {
throw new MissingEventRegistry($criterion::class);
}
if (!in_array($eventRegistry->eventName($message->event()::class), $criterion->events)) {
return false;
}
break;
default:
throw new UnsupportedCriterion($criterion::class);
}
}
return true;
},
ARRAY_FILTER_USE_BOTH,
);
}
public function clear(): void
{
$this->messages = [];
}
}