1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/cqrs-tools |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\CQRSTools\Domain\Event; |
11
|
|
|
|
12
|
|
|
use ReflectionClass; |
13
|
|
|
use Slick\CQRSTools\Event; |
14
|
|
|
use Slick\CQRSTools\Event\AbstractEvent; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* StoredEvent |
18
|
|
|
* |
19
|
|
|
* @package Slick\CQRSTools\Domain\Event |
20
|
|
|
*/ |
21
|
|
|
class StoredEvent extends AbstractEvent implements Event |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $eventClassName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|mixed |
31
|
|
|
*/ |
32
|
|
|
private $data = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates a stored event from other event |
36
|
|
|
* |
37
|
|
|
* @param Event $event |
38
|
|
|
* |
39
|
|
|
* @return StoredEvent |
40
|
|
|
*/ |
41
|
|
|
public static function createFromEvent(Event $event): StoredEvent |
42
|
|
|
{ |
43
|
|
|
if ($event instanceof StoredEvent) { |
44
|
|
|
return $event; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$stored = new StoredEvent($event->author()); |
48
|
|
|
$stored->eventId = $event->eventId(); |
49
|
|
|
$stored->occurredOn = $event->occurredOn(); |
50
|
|
|
$stored->eventClassName = get_class($event); |
51
|
|
|
$stored->data = json_encode($event); |
52
|
|
|
|
53
|
|
|
return $stored; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Specify data which should be serialized to JSON |
58
|
|
|
* |
59
|
|
|
* @return mixed data which can be serialized by json_encode(), |
60
|
|
|
* which is a value of any type other than a resource. |
61
|
|
|
*/ |
62
|
|
|
public function jsonSerialize() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'eventId' => (string) $this->eventId, |
66
|
|
|
'occurredOn' => $this->occurredOn->format('Y-m-d H:i:s.u'), |
67
|
|
|
'author' => (string) $this->author, |
68
|
|
|
'eventClassName' => $this->eventClassName, |
69
|
|
|
'data' => json_decode($this->data) |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Original event FQ Class name |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function eventClassName(): string |
79
|
|
|
{ |
80
|
|
|
return $this->eventClassName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Data from event serialization |
85
|
|
|
* |
86
|
|
|
* @return mixed|null |
87
|
|
|
*/ |
88
|
|
|
public function data() |
89
|
|
|
{ |
90
|
|
|
return $this->data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Event |
95
|
|
|
* @throws \ReflectionException |
96
|
|
|
*/ |
97
|
|
|
public function event(): Event |
98
|
|
|
{ |
99
|
|
|
$reflection = new ReflectionClass($this->eventClassName); |
100
|
|
|
/** @var Event $event */ |
101
|
|
|
$event = $reflection->newInstanceWithoutConstructor(); |
102
|
|
|
|
103
|
|
|
$properties = [ |
104
|
|
|
'author' => $this->author, 'occurredOn' => $this->occurredOn, 'eventId' => $this->eventId |
105
|
|
|
]; |
106
|
|
|
$event = $this->assignProperties($reflection, $properties, $event); |
107
|
|
|
$event->unserializeEvent(json_decode($this->data)); |
108
|
|
|
|
109
|
|
|
return $event; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function assignProperties(ReflectionClass $reflection, array $properties, Event $event): Event |
113
|
|
|
{ |
114
|
|
|
foreach ($properties as $name => $value) { |
115
|
|
|
if (!$reflection->hasProperty($name)) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$property = $reflection->getProperty($name); |
120
|
|
|
$property->setAccessible(true); |
121
|
|
|
$property->setValue($event, $value); |
122
|
|
|
$property->setAccessible(false); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $event; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Used to unserialize from a stored event |
130
|
|
|
* |
131
|
|
|
* @param mixed $data |
132
|
|
|
*/ |
133
|
|
|
public function unserializeEvent($data): void |
134
|
|
|
{ |
135
|
|
|
// do nothing! |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|