|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpDDD\DomainDrivenDesign\EventLog; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use PhpDDD\DomainDrivenDesign\Domain\AbstractAggregateRoot; |
|
7
|
|
|
use PhpDDD\DomainDrivenDesign\Event\AbstractEvent; |
|
8
|
|
|
use PhpDDD\DomainDrivenDesign\User\Author; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
final class EventLog extends AbstractAggregateRoot |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var mixed This will depend on how you save the object |
|
17
|
|
|
*/ |
|
18
|
|
|
private $id; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var mixed Identifier of the aggregate root |
|
22
|
|
|
*/ |
|
23
|
|
|
private $aggregateRootId; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Type of aggregate root. It should be a unique string that identify an aggregate root type. |
|
27
|
|
|
* For instance, you may use the Fully-Qualified Class Name. |
|
28
|
|
|
* Combined with the aggregate root id, you can point to a specific domain object. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $aggregateRootType; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The event name. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $eventName; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Events have some properties that vary depending on the event class. |
|
43
|
|
|
* These properties need to be stored. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string The event serialized |
|
46
|
|
|
*/ |
|
47
|
|
|
private $metadata; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Who request the action. |
|
51
|
|
|
* |
|
52
|
|
|
* @var Author |
|
53
|
|
|
*/ |
|
54
|
|
|
private $author; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var DateTime |
|
58
|
|
|
*/ |
|
59
|
|
|
private $occurredAt; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param mixed $aggregateRootId |
|
63
|
|
|
* @param string $aggregateRootType |
|
64
|
|
|
* @param Author $author |
|
65
|
|
|
* @param string $eventName |
|
66
|
|
|
* @param DateTime $occurredAt |
|
67
|
|
|
* @param string $metadata |
|
68
|
|
|
*/ |
|
69
|
|
|
private function __construct( |
|
70
|
|
|
$aggregateRootId, |
|
71
|
|
|
$aggregateRootType, |
|
72
|
|
|
Author $author, |
|
73
|
|
|
$eventName, |
|
74
|
|
|
DateTime $occurredAt, |
|
75
|
|
|
$metadata |
|
76
|
|
|
) { |
|
77
|
|
|
// Here we should create an Event like this: $this->apply(new LogEntryAdded(...); |
|
78
|
|
|
// But doing so will probably create an infinite loop when you want to subscribe to all events |
|
79
|
|
|
// Instead, we make an exception so you don't need to subscribe to every events except this one |
|
80
|
|
|
$this->aggregateRootId = $aggregateRootId; |
|
81
|
|
|
$this->aggregateRootType = $aggregateRootType; |
|
82
|
|
|
$this->author = $author; |
|
83
|
|
|
$this->eventName = $eventName; |
|
84
|
|
|
$this->occurredAt = $occurredAt; |
|
85
|
|
|
$this->metadata = $metadata; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param AbstractEvent $event |
|
90
|
|
|
* |
|
91
|
|
|
* @return EventLog |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function addEventEntry(AbstractEvent $event) |
|
94
|
|
|
{ |
|
95
|
|
|
return new self( |
|
96
|
|
|
null !== $event->aggregateRoot ? $event->aggregateRoot->getId() : null, |
|
97
|
|
|
null !== $event->aggregateRoot ? get_class($event->aggregateRoot) : null, |
|
98
|
|
|
$event->author, |
|
99
|
|
|
get_class($event), |
|
100
|
|
|
$event->date, |
|
101
|
|
|
serialize($event) |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get the unique identifier of this aggregate root. |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getId() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->id; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getAggregateRootId() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->aggregateRootId; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getAggregateRootType() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->aggregateRootType; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return Author |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getAuthor() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->author; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getEventName() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->eventName; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getMetadata() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->metadata; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return DateTime |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getOccurredAt() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->occurredAt; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|