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