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
|
|
|
* |
25
|
|
|
* @throws InvalidEventException |
26
|
|
|
*/ |
27
|
7 |
|
protected function persist(EventSourcedEntity $eventSourcedEntity, $event) |
28
|
|
|
{ |
29
|
7 |
|
if (false === array_key_exists($eventSourcedEntity->identifier(), $this->events)) { |
30
|
7 |
|
$this->events[$eventSourcedEntity->identifier()] = []; |
31
|
7 |
|
} |
32
|
|
|
|
33
|
7 |
|
$this->events[$eventSourcedEntity->identifier()][] = [ |
34
|
7 |
|
'entity_identifier' => $eventSourcedEntity->identifier(), |
35
|
7 |
|
'serial_number' => count($this->events[$eventSourcedEntity->identifier()]) + 1, |
36
|
7 |
|
'entity_class' => get_class($eventSourcedEntity), |
37
|
7 |
|
'recorded_at' => new \DateTime('now', new \DateTimeZone('UTC')), |
38
|
7 |
|
'event_class' => get_class($event), |
39
|
7 |
|
'event' => $this->serialize($event), |
40
|
|
|
]; |
41
|
|
|
|
42
|
7 |
|
array_push($this->stagedEvents, $event); |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $entityIdentifier |
47
|
|
|
* |
48
|
|
|
* @throws NoEventsFoundForKeyException |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
4 |
|
protected function eventLog($entityIdentifier) |
53
|
|
|
{ |
54
|
4 |
|
$this->verifyEventExistsForKey($entityIdentifier); |
55
|
|
|
|
56
|
3 |
|
return array_map(function ($eventLogEntry) { |
57
|
3 |
|
$eventLogEntry['event'] = $this->unserialize($eventLogEntry['event']); |
58
|
|
|
|
59
|
3 |
|
return $eventLogEntry; |
60
|
3 |
|
}, $this->events[$entityIdentifier]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
*/ |
65
|
7 |
|
protected function startTransaction(EventSourcedEntity $eventSourcedEntity) |
66
|
|
|
{ |
67
|
7 |
|
$this->transactionBackup = $this->events; |
68
|
|
|
|
69
|
7 |
|
$this->stagedEvents = []; |
70
|
7 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
*/ |
74
|
1 |
|
protected function abortTransaction(EventSourcedEntity $eventSourcedEntity) |
75
|
|
|
{ |
76
|
1 |
|
$this->events = $this->transactionBackup; |
77
|
1 |
|
$this->stagedEvents = []; |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
*/ |
82
|
7 |
|
protected function completeTransaction(EventSourcedEntity $eventSourcedEntity) |
83
|
|
|
{ |
84
|
7 |
|
$this->transactionBackup = []; |
85
|
|
|
|
86
|
7 |
|
$this->stagedEvents = []; |
87
|
7 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $entityIdentifier |
91
|
|
|
* |
92
|
|
|
* @throws NoEventsFoundForKeyException |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
8 |
|
protected function countEntityEvents($entityIdentifier) |
97
|
|
|
{ |
98
|
8 |
|
if (false === array_key_exists($entityIdentifier, $this->events)) { |
99
|
3 |
|
return 0; |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
return count($this->events[$entityIdentifier]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $entityIdentifier |
107
|
|
|
* |
108
|
|
|
* @throws NoEventsFoundForKeyException |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
2 |
|
public function entityClass($entityIdentifier) |
113
|
|
|
{ |
114
|
2 |
|
$this->verifyEventExistsForKey($entityIdentifier); |
115
|
|
|
|
116
|
1 |
|
return $this->events[$entityIdentifier][0]['entity_class']; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|