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
|
4 |
|
protected function persist(EventSourcedEntity $eventSourcedEntity) |
29
|
|
|
{ |
30
|
4 |
|
if (false === array_key_exists($eventSourcedEntity->identifier(), $this->events)) { |
31
|
4 |
|
$this->events[$eventSourcedEntity->identifier()] = []; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
array_map(function ($event) use ($eventSourcedEntity) { |
35
|
4 |
|
$this->events[$eventSourcedEntity->identifier()][] = [ |
36
|
4 |
|
'entity_identifier' => $eventSourcedEntity->identifier(), |
37
|
4 |
|
'serial_number' => count($this->events[$eventSourcedEntity->identifier()]) + 1, |
38
|
4 |
|
'entity_class' => get_class($eventSourcedEntity), |
39
|
4 |
|
'recorded_at' => new \DateTime('now', new \DateTimeZone('UTC')), |
40
|
4 |
|
'event_class' => get_class($event), |
41
|
4 |
|
'event' => $this->serialize($event), |
42
|
|
|
]; |
43
|
|
|
|
44
|
4 |
|
array_push($this->stagedEvents, $event); |
|
|
|
|
45
|
4 |
|
}, $eventSourcedEntity->stagedEvents()); |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $entityIdentifier |
50
|
|
|
* |
51
|
|
|
* @throws NoEventsFoundForKeyException |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
4 |
|
protected function eventLog($entityIdentifier) |
56
|
|
|
{ |
57
|
4 |
|
$this->verifyEventExistsForKey($entityIdentifier); |
58
|
|
|
|
59
|
3 |
|
return array_map(function ($eventLogEntry) { |
60
|
3 |
|
$eventLogEntry['event'] = $this->unserialize($eventLogEntry['event']); |
61
|
|
|
|
62
|
3 |
|
return $eventLogEntry; |
63
|
3 |
|
}, $this->events[$entityIdentifier]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
*/ |
68
|
4 |
|
protected function startTransaction() |
69
|
|
|
{ |
70
|
4 |
|
$this->transactionBackup = $this->events; |
71
|
4 |
|
$this->stagedEvents = []; |
|
|
|
|
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
*/ |
76
|
|
|
protected function abortTransaction() |
77
|
|
|
{ |
78
|
|
|
$this->events = $this->transactionBackup; |
79
|
|
|
$this->stagedEvents = []; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
*/ |
84
|
4 |
|
protected function completeTransaction() |
85
|
|
|
{ |
86
|
4 |
|
$this->transactionBackup = []; |
87
|
|
|
|
88
|
4 |
|
foreach ($this->stagedEvents as $event) { |
|
|
|
|
89
|
4 |
|
$this->publish(self::EVENT_STORED, $event); |
90
|
4 |
|
} |
91
|
|
|
|
92
|
4 |
|
$this->stagedEvents = []; |
|
|
|
|
93
|
4 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $entityIdentifier |
97
|
|
|
* |
98
|
|
|
* @throws NoEventsFoundForKeyException |
99
|
|
|
*/ |
100
|
4 |
|
protected function verifyEventExistsForKey($entityIdentifier) |
101
|
|
|
{ |
102
|
4 |
|
if (false === array_key_exists($entityIdentifier, $this->events)) { |
103
|
1 |
|
throw new NoEventsFoundForKeyException(); |
104
|
|
|
} |
105
|
3 |
|
} |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.