|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Blixit\EventSourcing\Store\InMemory; |
|
6
|
|
|
|
|
7
|
|
|
use Blixit\EventSourcing\Event\EventInterface; |
|
8
|
|
|
use Blixit\EventSourcing\Store\Matcher\EventMatcherInterface; |
|
9
|
|
|
use Blixit\EventSourcing\Store\Persistence\EventPersisterInterface; |
|
10
|
|
|
use Blixit\EventSourcing\Stream\StreamName; |
|
11
|
|
|
use function array_filter; |
|
12
|
|
|
use function end; |
|
13
|
|
|
|
|
14
|
|
|
class InMemoryEventPersister implements EventPersisterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var EventInterface[] $events */ |
|
17
|
|
|
private $events = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return EventInterface[] |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getByStream(StreamName $streamName, ?int $fromSequence = 0) : array |
|
23
|
|
|
{ |
|
24
|
|
|
return array_filter($this->events, static function (EventInterface $event) use ( |
|
25
|
|
|
$streamName, |
|
26
|
|
|
$fromSequence |
|
27
|
|
|
) { |
|
28
|
|
|
return $event->getStreamName() === (string) $streamName && $event->getSequence() > $fromSequence; |
|
29
|
|
|
}); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the last event stored into the stream |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getLastEvent(StreamName $streamName) : ?EventInterface |
|
36
|
|
|
{ |
|
37
|
|
|
return empty($this->events) ? null : end($this->events); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function persist(EventInterface $event) : EventInterface |
|
41
|
|
|
{ |
|
42
|
|
|
$this->events[] = $event; |
|
43
|
|
|
|
|
44
|
|
|
return $event; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return EventInterface[] |
|
49
|
|
|
*/ |
|
50
|
|
|
public function find(EventMatcherInterface $eventMatcher) : array |
|
51
|
|
|
{ |
|
52
|
|
|
return array_filter($this->events, static function (EventInterface $event) use ($eventMatcher) { |
|
53
|
|
|
$aggregateId = $eventMatcher->getAggregateId(); |
|
54
|
|
|
$aggregateClass = $eventMatcher->getAggregateClass(); |
|
55
|
|
|
$fromSequence = $eventMatcher->getSequence(); |
|
56
|
|
|
$streamName = $eventMatcher->getStreamName(); |
|
57
|
|
|
$timestamp = $eventMatcher->getTimestamp(); |
|
58
|
|
|
|
|
59
|
|
|
$condition = true; |
|
60
|
|
|
if (! empty($aggregateId)) { |
|
61
|
|
|
$condition = $condition && ($event->getAggregateId() === $aggregateId); |
|
62
|
|
|
} |
|
63
|
|
|
if (! empty($aggregateClass)) { |
|
64
|
|
|
$condition = $condition && ($event->getAggregateClass() === (string) $aggregateClass); |
|
65
|
|
|
} |
|
66
|
|
|
if (! empty($fromSequence)) { |
|
67
|
|
|
$condition = $condition && ($event->getSequence() > $fromSequence); |
|
68
|
|
|
} |
|
69
|
|
|
if (! empty($streamName)) { |
|
70
|
|
|
$condition = $condition && ($event->getStreamName() === (string) $streamName); |
|
71
|
|
|
} |
|
72
|
|
|
if (! empty($timestamp)) { |
|
73
|
|
|
$condition = $condition && ($event->getTimestamp() > $timestamp); |
|
74
|
|
|
} |
|
75
|
|
|
return $condition; |
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|