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