1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawkode\Eidetic\EventStore; |
4
|
|
|
|
5
|
|
|
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity; |
6
|
|
|
|
7
|
|
|
abstract class EventStore implements Serializer |
8
|
|
|
{ |
9
|
|
|
use EventPublisherMixin; |
10
|
|
|
|
11
|
|
|
// Subscriber hooks |
12
|
|
|
const EVENT_STORED = 'eidetic.eventstore.event_stored'; |
13
|
|
|
|
14
|
|
|
// Implement these in your concretion |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param EventSourcedEntity $eventSourcedEntity |
18
|
|
|
* |
19
|
|
|
* @throws InvalidEventException |
20
|
|
|
*/ |
21
|
|
|
abstract protected function persist(EventSourcedEntity $eventSourcedEntity); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return all event log entries for $entityIdentifier. |
25
|
|
|
* |
26
|
|
|
* @param string $entityIdentifier |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
abstract protected function eventLog($entityIdentifier); |
31
|
|
|
|
32
|
|
|
abstract protected function startTransaction(); |
33
|
|
|
abstract protected function abortTransaction(); |
34
|
|
|
abstract protected function completeTransaction(); |
35
|
|
|
|
36
|
|
|
abstract protected function countEntityEvents($entityIdentifier); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the class associated with an entity identifier. |
40
|
|
|
* |
41
|
|
|
* @param string $entityIdentifier |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
abstract protected function entityClass($entityIdentifier); |
46
|
|
|
|
47
|
|
|
/** @var array */ |
48
|
|
|
protected $stagedEvents = []; |
49
|
|
|
|
50
|
|
|
/** @var Serializer */ |
51
|
|
|
protected $serializer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Store an EventSourcedEntity's staged events. |
55
|
|
|
* |
56
|
|
|
* @param EventSourcedEntity $eventSourcedEntity |
57
|
|
|
*/ |
58
|
10 |
|
public function store(EventSourcedEntity $eventSourcedEntity) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
10 |
|
$this->startTransaction(); |
62
|
10 |
|
$this->persist($eventSourcedEntity); |
63
|
10 |
|
} catch (\Exception $exception) { |
64
|
|
|
$this->abortTransaction(); |
65
|
|
|
throw $exception; |
66
|
|
|
} |
67
|
|
|
|
68
|
10 |
|
$this->completeTransaction(); |
69
|
10 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all events for $entityIdentifier. |
73
|
|
|
* |
74
|
|
|
* @param string $entityIdentifier |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
4 |
|
public function retrieve($entityIdentifier) |
79
|
|
|
{ |
80
|
4 |
|
$eventLog = $this->eventLog($entityIdentifier); |
81
|
|
|
|
82
|
2 |
|
return array_map(function ($eventLogEntry) { |
83
|
2 |
|
return $eventLogEntry['event']; |
84
|
2 |
|
}, $eventLog); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns all the log entries for $entityIdentifier. |
89
|
|
|
* |
90
|
|
|
* @param string $entityIdentifier |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
4 |
|
public function retrieveLog($entityIdentifier) |
95
|
|
|
{ |
96
|
4 |
|
return $this->eventLog($entityIdentifier); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $entityIdentifier |
101
|
|
|
* |
102
|
|
|
* @throws NoEventsFoundForKeyException |
103
|
|
|
*/ |
104
|
4 |
|
protected function verifyEventExistsForKey($entityIdentifier) |
105
|
|
|
{ |
106
|
4 |
|
if (0 === $this->countEntityEvents($entityIdentifier)) { |
107
|
1 |
|
throw new NoEventsFoundForKeyException(); |
108
|
|
|
} |
109
|
3 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function stagedEvents() |
115
|
|
|
{ |
116
|
|
|
return $this->stagedEvents; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param object $object |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
10 |
|
public function serialize($object) |
125
|
|
|
{ |
126
|
10 |
|
if (false === is_null($this->serializer)) { |
127
|
|
|
return $this->serializer->serialize($object); |
128
|
|
|
} |
129
|
|
|
|
130
|
10 |
|
return base64_encode(serialize($object)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $serializedObject |
135
|
|
|
* |
136
|
|
|
* @return object |
137
|
|
|
*/ |
138
|
6 |
|
public function unserialize($serializedObject) |
139
|
|
|
{ |
140
|
6 |
|
if (false === is_null($this->serializer)) { |
141
|
|
|
return $this->serializer->serialize($serializedObject); |
142
|
|
|
} |
143
|
|
|
|
144
|
6 |
|
return unserialize(base64_decode($serializedObject)); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|