1 | <?php |
||
17 | class InMemoryEventStore implements EventStore |
||
18 | { |
||
19 | /** @var EventsCommit[] */ |
||
20 | public $commitsByAggregate = []; |
||
21 | private $versions = []; |
||
22 | private $latestSequence = 0; |
||
23 | |||
24 | 10 | public function loadEventsForAggregate(string $aggregateClass, $aggregateId): AggregateEventStream |
|
29 | |||
30 | /** |
||
31 | * @inheritdoc |
||
32 | */ |
||
33 | 10 | public function appendEventsForAggregate($aggregateId, string $aggregateClass, $eventsWithMetaData, int $expectedVersion, int $expectedSequence) |
|
41 | |||
42 | 11 | public function appendEventsForAggregateWithoutChecking($aggregateId, $aggregateClass, $newEvents, int $expectedVersion, int $expectedSequence) |
|
43 | { |
||
44 | 11 | $this->addEventsToArrayForAggregate( |
|
45 | 11 | $aggregateId, |
|
46 | 11 | $aggregateClass, |
|
47 | 11 | $this->decorateEventsWithMetadata($aggregateClass, $aggregateId, $newEvents), |
|
48 | 11 | $expectedVersion, |
|
49 | 11 | $expectedSequence |
|
50 | ); |
||
51 | |||
52 | 11 | $constructKey = $this->constructKey($aggregateClass, $aggregateId); |
|
53 | |||
54 | 11 | if (!isset($this->versions[$constructKey])) { |
|
55 | 11 | $this->versions[$constructKey] = 0; |
|
56 | } |
||
57 | |||
58 | 11 | $this->versions[$constructKey]++; |
|
59 | 11 | $this->latestSequence++; |
|
60 | 11 | } |
|
61 | |||
62 | 10 | private function getEventsArrayForAggregate(string $aggregateClass, $aggregateId) |
|
70 | |||
71 | 11 | private function addEventsToArrayForAggregate($aggregateId, $aggregateClass, $newEvents, int $expectedVersion, int $expectedSequence) |
|
72 | { |
||
73 | 11 | $this->commitsByAggregate[$this->constructKey($aggregateClass, $aggregateId)][] = new EventsCommit( |
|
74 | 11 | $expectedSequence, $expectedVersion, $newEvents |
|
75 | ); |
||
76 | 11 | } |
|
77 | |||
78 | public function loadEventsByClassNames(array $eventClasses): EventStreamGroupedByCommit |
||
86 | |||
87 | private function extractEventsFromCommits(array $commits = []) |
||
95 | |||
96 | 11 | public function getAggregateVersion(string $aggregateClass, $aggregateId) |
|
102 | |||
103 | /** |
||
104 | * @param $aggregateClass |
||
105 | * @param $aggregateId |
||
106 | * @param $priorEvents |
||
107 | * @return EventWithMetaData[] |
||
108 | */ |
||
109 | public function decorateEventsWithMetadata($aggregateClass, $aggregateId, array $priorEvents) |
||
110 | { |
||
111 | 11 | return array_map(function ($event) use ($aggregateClass, $aggregateId) { |
|
112 | 11 | if ($event instanceof EventWithMetaData) { |
|
113 | 10 | return $event; |
|
114 | } |
||
115 | |||
116 | 5 | return new EventWithMetaData($event, new MetaData( |
|
117 | 5 | $aggregateId, |
|
118 | 5 | $aggregateClass, |
|
119 | 5 | new \DateTimeImmutable(), |
|
120 | 5 | null |
|
121 | )); |
||
122 | 11 | }, $priorEvents); |
|
123 | } |
||
124 | |||
125 | 3 | public function fetchLatestSequence(): int |
|
129 | |||
130 | 14 | private function constructKey(string $aggregateClass, $aggregateId): string |
|
134 | |||
135 | 1 | public function findEventById(string $eventId): ?EventWithMetaData |
|
136 | { |
||
137 | 1 | foreach($this->commitsByAggregate as $commits) |
|
156 | } |
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: