1
|
|
|
<?php |
2
|
|
|
namespace Cmp\Queues\Domain\Event; |
3
|
|
|
|
4
|
|
|
use Cmp\Queues\Domain\Event\Exception\DomainEventException; |
5
|
|
|
use Cmp\Queues\Domain\Queue\Message; |
6
|
|
|
|
7
|
|
|
class DomainEvent implements Message |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $origin; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $version; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $occurredOn; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $body = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $isDeprecated = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $origin |
41
|
|
|
* @param string $name |
42
|
|
|
* @param string $version |
43
|
|
|
* @param int $occurredOn |
44
|
|
|
* @param array $body |
45
|
|
|
* @param bool $isDeprecated |
46
|
|
|
*/ |
47
|
|
|
public function __construct($origin, $name, $version, $occurredOn, array $body = [], $isDeprecated = false) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->setOrigin($origin) |
50
|
|
|
->setName($name) |
51
|
|
|
->setVersion($version) |
52
|
|
|
->setOccurredOn($occurredOn) |
53
|
|
|
; |
54
|
|
|
$this->body = $body; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getOrigin() |
61
|
|
|
{ |
62
|
|
|
return $this->origin; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getName() |
69
|
|
|
{ |
70
|
|
|
return $this->name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getVersion() |
77
|
|
|
{ |
78
|
|
|
return $this->version; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Timestamp |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function getOccurredOn() |
87
|
|
|
{ |
88
|
|
|
return $this->occurredOn; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getBody() |
95
|
|
|
{ |
96
|
|
|
return $this->body; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getDelay() |
103
|
|
|
{ |
104
|
|
|
return 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function isDeprecated() |
111
|
|
|
{ |
112
|
|
|
return $this->isDeprecated; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $origin |
117
|
|
|
* @return DomainEvent $this |
118
|
|
|
* @throws DomainEventException |
119
|
|
|
*/ |
120
|
|
|
protected function setOrigin($origin) |
121
|
|
|
{ |
122
|
|
|
if(empty($origin)) { |
123
|
|
|
throw new DomainEventException('DomainEvent origin cannot be empty'); |
124
|
|
|
} |
125
|
|
|
$this->origin = $origin; |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* @return DomainEvent $this |
132
|
|
|
* @throws DomainEventException |
133
|
|
|
*/ |
134
|
|
|
protected function setName($name) |
135
|
|
|
{ |
136
|
|
|
if(empty($name)) { |
137
|
|
|
throw new DomainEventException('DomainEvent name cannot be empty'); |
138
|
|
|
} |
139
|
|
|
$this->name = $name; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $version |
145
|
|
|
* @return DomainEvent $this |
146
|
|
|
* @throws DomainEventException |
147
|
|
|
*/ |
148
|
|
|
protected function setVersion($version) |
149
|
|
|
{ |
150
|
|
|
if(empty($version)) { |
151
|
|
|
throw new DomainEventException('DomainEvent version cannot be empty'); |
152
|
|
|
} |
153
|
|
|
$this->version = $version; |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param int $occurredOn |
159
|
|
|
* @return DomainEvent $this |
160
|
|
|
* @throws DomainEventException |
161
|
|
|
*/ |
162
|
|
|
protected function setOccurredOn($occurredOn) |
163
|
|
|
{ |
164
|
|
|
if(!is_null($occurredOn) && !preg_match('/^\d+(\.\d{1,4})?$/', $occurredOn)) { // accepts also microseconds |
165
|
|
|
throw new DomainEventException("$occurredOn is not a valid unix timestamp."); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($occurredOn > time()) { |
169
|
|
|
throw new DomainEventException('OccuredOn cannot be located in the future'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->occurredOn = $occurredOn; |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function jsonSerialize() |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
'origin' => $this->origin, |
183
|
|
|
'name' => $this->name, |
184
|
|
|
'version' => $this->version, |
185
|
|
|
'occurredOn' => $this->occurredOn, |
186
|
|
|
'body' => $this->body, |
187
|
|
|
'isDeprecated' => $this->isDeprecated, |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.