|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawkode\Eidetic\EventStore\InMemoryEventStore; |
|
4
|
|
|
|
|
5
|
|
|
use Rawkode\Eidetic\EventStore\InvalidEventException; |
|
6
|
|
|
use Rawkode\Eidetic\EventStore\EventStore; |
|
7
|
|
|
use Rawkode\Eidetic\EventStore\NoEventsFoundForKeyException; |
|
8
|
|
|
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity; |
|
9
|
|
|
|
|
10
|
|
|
final class InMemoryEventStore extends EventStore |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $events = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $transactionBackup = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param EventSourcedEntity $eventSourcedEntity |
|
24
|
|
|
* @param object $event |
|
|
|
|
|
|
25
|
|
|
* |
|
26
|
|
|
* @throws InvalidEventException |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function persist(EventSourcedEntity $eventSourcedEntity) |
|
29
|
|
|
{ |
|
30
|
|
|
if (false === array_key_exists($eventSourcedEntity->identifier(), $this->events)) { |
|
31
|
|
|
$this->events[$eventSourcedEntity->identifier()] = []; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
array_map(function ($event) use ($eventSourcedEntity) { |
|
35
|
|
|
$this->events[$eventSourcedEntity->identifier()][] = [ |
|
36
|
|
|
'entity_identifier' => $eventSourcedEntity->identifier(), |
|
37
|
|
|
'serial_number' => count($this->events[$eventSourcedEntity->identifier()]) + 1, |
|
38
|
|
|
'entity_class' => get_class($eventSourcedEntity), |
|
39
|
|
|
'recorded_at' => new \DateTime('now', new \DateTimeZone('UTC')), |
|
40
|
|
|
'event_class' => get_class($event), |
|
41
|
|
|
'event' => $this->serialize($event), |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
array_push($this->stagedEvents, $event); |
|
|
|
|
|
|
45
|
2 |
|
}, $eventSourcedEntity->stagedEvents()); |
|
46
|
|
|
} |
|
47
|
2 |
|
|
|
48
|
|
|
/** |
|
49
|
1 |
|
* @param string $entityIdentifier |
|
50
|
1 |
|
* |
|
51
|
1 |
|
* @throws NoEventsFoundForKeyException |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function eventLog($entityIdentifier) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->verifyEventExistsForKey($entityIdentifier); |
|
58
|
|
|
|
|
59
|
1 |
|
return array_map(function ($eventLogEntry) { |
|
60
|
|
|
$eventLogEntry['event'] = $this->unserialize($eventLogEntry['event']); |
|
61
|
1 |
|
|
|
62
|
|
|
return $eventLogEntry; |
|
63
|
|
|
}, $this->events[$entityIdentifier]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function startTransaction() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->transactionBackup = $this->events; |
|
71
|
3 |
|
$this->stagedEvents = []; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
3 |
|
|
|
74
|
|
|
/** |
|
75
|
2 |
|
*/ |
|
76
|
|
|
protected function abortTransaction() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->events = $this->transactionBackup; |
|
79
|
|
|
$this->stagedEvents = []; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function completeTransaction() |
|
85
|
5 |
|
{ |
|
86
|
|
|
$this->transactionBackup = []; |
|
87
|
|
|
|
|
88
|
5 |
|
foreach ($this->stagedEvents as $event) { |
|
|
|
|
|
|
89
|
|
|
$this->publish(self::EVENT_STORED, $event); |
|
90
|
5 |
|
} |
|
91
|
5 |
|
|
|
92
|
5 |
|
$this->stagedEvents = []; |
|
|
|
|
|
|
93
|
5 |
|
} |
|
94
|
2 |
|
|
|
95
|
|
|
/** |
|
96
|
2 |
|
* @param string $entityIdentifier |
|
97
|
|
|
* |
|
98
|
|
|
* @throws NoEventsFoundForKeyException |
|
99
|
4 |
|
*/ |
|
100
|
4 |
|
protected function verifyEventExistsForKey($entityIdentifier) |
|
101
|
|
|
{ |
|
102
|
|
|
if (false === array_key_exists($entityIdentifier, $this->events)) { |
|
103
|
|
|
throw new NoEventsFoundForKeyException(); |
|
104
|
|
|
} |
|
105
|
5 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.