1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawkode\Eidetic\EventSourcing; |
4
|
|
|
|
5
|
|
|
trait EventSourcedEntityMixin |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $identifier; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $version = 0; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $stagedEvents = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function identifier() |
26
|
|
|
{ |
27
|
|
|
return $this->identifier; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
2 |
|
public function version() |
34
|
|
|
{ |
35
|
2 |
|
return $this->version; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $eventStream |
40
|
|
|
*/ |
41
|
1 |
|
public static function initialise(array $eventStream) |
42
|
|
|
{ |
43
|
1 |
|
$entity = new self; |
44
|
|
|
|
45
|
1 |
|
foreach ($eventStream as $event) { |
46
|
1 |
|
$entity->applyEvent($event); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
$entity->commit(); |
50
|
|
|
|
51
|
1 |
|
return $entity; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
3 |
|
public function stagedEvents() |
58
|
|
|
{ |
59
|
3 |
|
return $this->stagedEvents; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
*/ |
64
|
2 |
|
public function commit() |
65
|
|
|
{ |
66
|
2 |
|
$this->version += count($this->stagedEvents); |
67
|
2 |
|
$this->stagedEvents = []; |
68
|
2 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param object $event |
72
|
|
|
*/ |
73
|
3 |
|
private function applyEvent($event) |
74
|
|
|
{ |
75
|
3 |
|
$applyMethod = $this->findEventHandler($event); |
76
|
|
|
|
77
|
3 |
|
array_push($this->stagedEvents, $event); |
78
|
|
|
|
79
|
3 |
|
$this->$applyMethod($event); |
80
|
3 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param object $event |
84
|
|
|
* |
85
|
|
|
* @throws EventHandlerDoesNotExist |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
4 |
|
private function findEventHandler($event) |
90
|
|
|
{ |
91
|
4 |
|
$class = get_class($event); |
92
|
4 |
|
$explode = explode('\\', $class); |
93
|
4 |
|
$applyMethod = 'apply'.end($explode); |
94
|
|
|
|
95
|
4 |
|
if (!method_exists($this, $applyMethod)) { |
96
|
|
|
throw new EventHandlerDoesNotExistException("Couldn't find event handler for '{$applyMethod}'"); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
return $applyMethod; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return EventSourcedEntityMixin |
|
|
|
|
104
|
|
|
*/ |
105
|
|
|
public static function getClass() |
106
|
|
|
{ |
107
|
|
|
return new self; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.