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
|
|
|
/** @var array */ |
37
|
|
|
protected $stagedEvents = []; |
38
|
|
|
|
39
|
|
|
/** @var Serializer */ |
40
|
|
|
protected $serializer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Store an EventSourcedEntity's staged events. |
44
|
|
|
* |
45
|
|
|
* @param EventSourcedEntity $eventSourcedEntity |
46
|
|
|
*/ |
47
|
8 |
|
public function store(EventSourcedEntity $eventSourcedEntity) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
8 |
|
$this->startTransaction(); |
51
|
8 |
|
$this->persist($eventSourcedEntity); |
52
|
8 |
|
} catch (\Exception $exception) { |
53
|
|
|
$this->abortTransaction(); |
54
|
|
|
throw $exception; |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
$this->completeTransaction(); |
58
|
8 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns all events for $entityIdentifier. |
62
|
|
|
* |
63
|
|
|
* @param string $entityIdentifier |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
4 |
|
public function retrieve($entityIdentifier) |
68
|
|
|
{ |
69
|
4 |
|
$eventLog = $this->eventLog($entityIdentifier); |
70
|
|
|
|
71
|
2 |
|
return array_map(function ($eventLogEntry) { |
72
|
2 |
|
return $eventLogEntry['event']; |
73
|
2 |
|
}, $eventLog); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns all the log entries for $entityIdentifier. |
78
|
|
|
* |
79
|
|
|
* @param string $entityIdentifier |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
4 |
|
public function retrieveLog($entityIdentifier) |
84
|
|
|
{ |
85
|
4 |
|
return $this->eventLog($entityIdentifier); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function stagedEvents() |
92
|
|
|
{ |
93
|
|
|
return $this->stagedEvents; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param object $object |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
8 |
|
public function serialize($object) |
102
|
|
|
{ |
103
|
8 |
|
if (false === is_null($this->serializer)) { |
104
|
|
|
return $this->serializer->serialize($object); |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
return base64_encode(serialize($object)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $serializedObject |
112
|
|
|
* |
113
|
|
|
* @return object |
114
|
|
|
*/ |
115
|
6 |
|
public function unserialize($serializedObject) |
116
|
|
|
{ |
117
|
6 |
|
if (false === is_null($this->serializer)) { |
118
|
|
|
return $this->serializer->serialize($serializedObject); |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
return unserialize(base64_decode($serializedObject)); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|