|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DDDominio\EventSourcing\EventStore; |
|
4
|
|
|
|
|
5
|
|
|
use DDDominio\EventSourcing\Common\EventStream; |
|
6
|
|
|
use DDDominio\EventSourcing\Common\EventStreamInterface; |
|
7
|
|
|
use DDDominio\EventSourcing\Serialization\SerializerInterface; |
|
8
|
|
|
use DDDominio\EventSourcing\Versioning\EventUpgrader; |
|
9
|
|
|
use DDDominio\EventSourcing\Versioning\Version; |
|
10
|
|
|
|
|
11
|
|
|
class InMemoryEventStore extends AbstractEventStore |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var StoredEventStream[] |
|
15
|
|
|
*/ |
|
16
|
|
|
private $streams; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param SerializerInterface $serializer |
|
20
|
|
|
* @param EventUpgrader $eventUpgrader |
|
21
|
|
|
* @param StoredEventStream[] $streams |
|
22
|
|
|
*/ |
|
23
|
39 |
|
public function __construct( |
|
24
|
|
|
$serializer, |
|
25
|
|
|
$eventUpgrader, |
|
26
|
|
|
array $streams = [] |
|
27
|
|
|
) { |
|
28
|
39 |
|
parent::__construct($serializer, $eventUpgrader); |
|
29
|
39 |
|
$this->streams = $streams; |
|
30
|
39 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $streamId |
|
34
|
|
|
* @param StoredEvent[] $storedEvents |
|
35
|
|
|
* @param int $expectedVersion |
|
36
|
|
|
*/ |
|
37
|
14 |
|
protected function appendStoredEvents($streamId, $storedEvents, $expectedVersion) |
|
38
|
|
|
{ |
|
39
|
14 |
|
if ($this->streamExists($streamId)) { |
|
40
|
4 |
|
$this->streams[$streamId] = $this->streams[$streamId]->append($storedEvents); |
|
41
|
|
|
} else { |
|
42
|
11 |
|
$this->streams[$streamId] = new StoredEventStream($streamId, $storedEvents); |
|
43
|
|
|
} |
|
44
|
14 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $streamId |
|
48
|
|
|
* @return EventStreamInterface |
|
49
|
|
|
*/ |
|
50
|
18 |
|
public function readFullStream($streamId) |
|
51
|
|
|
{ |
|
52
|
18 |
|
if ($this->streamExists($streamId)) { |
|
53
|
14 |
|
return $this->domainEventStreamFromStoredEvents( |
|
54
|
14 |
|
$this->streams[$streamId]->events() |
|
|
|
|
|
|
55
|
|
|
); |
|
56
|
|
|
} else { |
|
57
|
4 |
|
return EventStream::buildEmpty(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $streamId |
|
63
|
|
|
* @param int $start |
|
64
|
|
|
* @param int $count |
|
65
|
|
|
* @return EventStreamInterface |
|
66
|
|
|
*/ |
|
67
|
6 |
|
public function readStreamEvents($streamId, $start = 1, $count = null) |
|
68
|
|
|
{ |
|
69
|
6 |
|
if (!$this->streamExists($streamId)) { |
|
70
|
1 |
|
return EventStream::buildEmpty(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
$storedEvents = $this->streams[$streamId]->events(); |
|
74
|
|
|
|
|
75
|
5 |
|
if (isset($count)) { |
|
76
|
2 |
|
$filteredStoredEvents = array_splice($storedEvents, $start - 1, $count); |
|
77
|
|
|
} else { |
|
78
|
3 |
|
$filteredStoredEvents = array_splice($storedEvents, $start - 1); |
|
79
|
|
|
} |
|
80
|
5 |
|
return $this->domainEventStreamFromStoredEvents($filteredStoredEvents); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $streamId |
|
85
|
|
|
* @param \DateTimeImmutable $datetime |
|
86
|
|
|
* @param int $start |
|
87
|
|
|
* @return EventStreamInterface |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function readStreamEventsUntil($streamId, $datetime, $start = 1) |
|
90
|
|
|
{ |
|
91
|
4 |
|
if (!$this->streamExists($streamId)) { |
|
92
|
1 |
|
return EventStream::buildEmpty(); |
|
93
|
|
|
} |
|
94
|
3 |
|
$storedEvents = $this->streams[$streamId]->events(); |
|
95
|
|
|
|
|
96
|
3 |
|
$filteredStoredEvents = array_splice($storedEvents, $start - 1); |
|
97
|
|
|
$filteredStoredEvents = array_filter($filteredStoredEvents, function(StoredEvent $event) use ($datetime) { |
|
98
|
3 |
|
return $event->occurredOn()->getTimestamp() <= $datetime->getTimestamp(); |
|
99
|
3 |
|
}); |
|
100
|
|
|
|
|
101
|
3 |
|
return $this->domainEventStreamFromStoredEvents($filteredStoredEvents); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return EventStreamInterface[] |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function readAllStreams() |
|
108
|
|
|
{ |
|
109
|
2 |
|
$allStreams = []; |
|
110
|
2 |
|
foreach ($this->streams as $stream) { |
|
111
|
1 |
|
$allStreams[] = $this->domainEventStreamFromStoredEvents($stream->events()); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
2 |
|
return $allStreams; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return EventStreamInterface |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function readAllEvents() |
|
120
|
|
|
{ |
|
121
|
1 |
|
$allEventsStream = EventStream::buildEmpty(); |
|
122
|
1 |
|
foreach ($this->streams as $stream) { |
|
123
|
1 |
|
$streamEvents = $this->domainEventStreamFromStoredEvents($stream->events()); |
|
|
|
|
|
|
124
|
1 |
|
$allEventsStream = $allEventsStream->append($streamEvents->events()); |
|
125
|
|
|
} |
|
126
|
1 |
|
return $allEventsStream; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $streamId |
|
131
|
|
|
* @return int |
|
132
|
|
|
*/ |
|
133
|
4 |
|
protected function streamVersion($streamId) |
|
134
|
|
|
{ |
|
135
|
4 |
|
return $this->streamExists($streamId) ? |
|
136
|
4 |
|
count($this->streams[$streamId]->events()) : 0; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $type |
|
141
|
|
|
* @param Version $version |
|
142
|
|
|
* @return EventStreamInterface |
|
143
|
|
|
*/ |
|
144
|
1 |
|
protected function readStoredEventsOfTypeAndVersion($type, $version) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$storedEvents = []; |
|
147
|
1 |
|
foreach ($this->streams as $stream) { |
|
148
|
|
|
/** @var StoredEvent $event */ |
|
149
|
1 |
|
foreach ($stream as $event) { |
|
150
|
1 |
|
if ($event->type() === $type && $event->version()->equalTo($version)) { |
|
151
|
1 |
|
$storedEvents[] = $event; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
1 |
|
return new EventStream($storedEvents); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $streamId |
|
160
|
|
|
* @return bool |
|
161
|
|
|
*/ |
|
162
|
38 |
|
protected function streamExists($streamId) |
|
163
|
|
|
{ |
|
164
|
38 |
|
return isset($this->streams[$streamId]); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $streamId |
|
169
|
|
|
* @param \DateTimeImmutable $datetime |
|
170
|
|
|
* @return int |
|
171
|
|
|
* @throws EventStreamDoesNotExistException |
|
172
|
|
|
*/ |
|
173
|
4 |
|
public function getStreamVersionAt($streamId, \DateTimeImmutable $datetime) |
|
174
|
|
|
{ |
|
175
|
4 |
|
if (!$this->streamExists($streamId)) { |
|
176
|
1 |
|
throw EventStreamDoesNotExistException::fromStreamId($streamId); |
|
177
|
|
|
} |
|
178
|
3 |
|
$storedEvents = $this->streams[$streamId]->events(); |
|
179
|
|
|
|
|
180
|
3 |
|
$filteredStoredEvents = array_filter($storedEvents, function(StoredEvent $event) use ($datetime) { |
|
181
|
3 |
|
return $event->occurredOn()->getTimestamp() <= $datetime->getTimestamp(); |
|
182
|
3 |
|
}); |
|
183
|
|
|
|
|
184
|
3 |
|
return count($filteredStoredEvents); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: