-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathInMemoryStreamSource.php
More file actions
57 lines (46 loc) · 1.52 KB
/
InMemoryStreamSource.php
File metadata and controls
57 lines (46 loc) · 1.52 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
<?php
/*
* licence Enterprise
*/
declare(strict_types=1);
namespace Ecotone\Projecting\InMemory;
use Ecotone\Modelling\Event;
use Ecotone\Projecting\StreamPage;
use Ecotone\Projecting\StreamSource;
use function in_array;
class InMemoryStreamSource implements StreamSource
{
/**
* @param string[]|null $handledProjectionNames null means handles all projections
* @param Event[] $events
*/
public function __construct(
private ?array $handledProjectionNames = null,
private ?string $partitionHeader = null,
private array $events = [],
) {
}
public function canHandle(string $projectionName): bool
{
return $this->handledProjectionNames === null
|| in_array($projectionName, $this->handledProjectionNames, true);
}
public function append(Event ...$events): void
{
foreach ($events as $event) {
$this->events[] = $event;
}
}
public function load(string $projectionName, ?string $lastPosition, int $count, ?string $partitionKey, string $streamName): StreamPage
{
$from = $lastPosition !== null ? (int) $lastPosition : 0;
if ($partitionKey) {
$events = array_filter($this->events, fn (Event $event) => $event->getMetadata()[$this->partitionHeader] === $partitionKey);
} else {
$events = $this->events;
}
$events = array_slice($events, $from, $count);
$to = $from + count($events);
return new StreamPage($events, (string) $to);
}
}