|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shippinno\Notification\Domain\Model; |
|
5
|
|
|
|
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
|
|
10
|
|
|
class Notification |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var null|NotificationId |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $notificationId; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Destination |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $destination; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Subject |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $subject; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Body |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $body; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var null|DeduplicationKey |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $deduplicationKey; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $metadata; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var DateTimeImmutable |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $createdAt; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var null|DateTimeImmutable |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $sentAt; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var null|DateTimeImmutable |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $failedAt; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var null|string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $failedFor; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var null|DateTimeImmutable |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $lockedAt; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param Destination $destination |
|
69
|
|
|
* @param Subject $subject |
|
70
|
|
|
* @param Body $body |
|
71
|
|
|
* @param DeduplicationKey|null $deduplicationKey |
|
72
|
|
|
* @param string|null $templateName |
|
|
|
|
|
|
73
|
|
|
* @param array|null $templateVariables |
|
|
|
|
|
|
74
|
|
|
* @param array|null $metadata |
|
75
|
|
|
*/ |
|
76
|
15 |
|
public function __construct( |
|
77
|
|
|
Destination $destination, |
|
78
|
|
|
Subject $subject, |
|
79
|
|
|
Body $body, |
|
80
|
|
|
DeduplicationKey $deduplicationKey = null, |
|
81
|
|
|
array $metadata = null |
|
82
|
|
|
) { |
|
83
|
15 |
|
$this->destination = $destination; |
|
84
|
15 |
|
$this->subject = $subject; |
|
85
|
15 |
|
$this->body = $body; |
|
86
|
15 |
|
$this->deduplicationKey = $deduplicationKey; |
|
87
|
15 |
|
$this->metadata = $metadata; |
|
88
|
15 |
|
$this->createdAt = new DateTimeImmutable; |
|
89
|
15 |
|
$this->sentAt = null; |
|
90
|
15 |
|
$this->failedAt = null; |
|
91
|
15 |
|
$this->failedFor = null; |
|
92
|
15 |
|
$this->lockedAt = null; |
|
93
|
15 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return null|NotificationId |
|
97
|
|
|
*/ |
|
98
|
3 |
|
public function notificationId(): ?NotificationId |
|
99
|
|
|
{ |
|
100
|
3 |
|
return $this->notificationId; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Destination |
|
105
|
|
|
*/ |
|
106
|
11 |
|
public function destination(): Destination |
|
107
|
|
|
{ |
|
108
|
11 |
|
return $this->destination; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Subject |
|
113
|
|
|
*/ |
|
114
|
6 |
|
public function subject(): Subject |
|
115
|
|
|
{ |
|
116
|
6 |
|
return $this->subject; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return Body |
|
121
|
|
|
*/ |
|
122
|
4 |
|
public function body(): Body |
|
123
|
|
|
{ |
|
124
|
4 |
|
return $this->body; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return null|DeduplicationKey |
|
129
|
|
|
*/ |
|
130
|
5 |
|
public function deduplicationKey(): ?DeduplicationKey |
|
131
|
|
|
{ |
|
132
|
5 |
|
return $this->deduplicationKey; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return array|null |
|
137
|
|
|
*/ |
|
138
|
|
|
public function metadata(): ?array |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->metadata; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param array $metadata |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setMetadata(array $metadata): void |
|
147
|
|
|
{ |
|
148
|
|
|
$this->metadata = $metadata; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $key |
|
153
|
|
|
* @param string|array $value |
|
154
|
|
|
*/ |
|
155
|
|
|
public function addMetadata(string $key, $value): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->metadata[$key] = $value; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return DateTimeImmutable |
|
162
|
|
|
*/ |
|
163
|
3 |
|
public function createdAt(): DateTimeImmutable |
|
164
|
|
|
{ |
|
165
|
3 |
|
return $this->createdAt; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return DateTimeImmutable|null |
|
170
|
|
|
*/ |
|
171
|
7 |
|
public function sentAt(): ?DateTimeImmutable |
|
172
|
|
|
{ |
|
173
|
7 |
|
return $this->sentAt; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
7 |
|
public function isSent(): bool |
|
180
|
|
|
{ |
|
181
|
7 |
|
return !is_null($this->sentAt()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return void |
|
186
|
|
|
*/ |
|
187
|
5 |
|
public function markSent(): void |
|
188
|
|
|
{ |
|
189
|
5 |
|
$this->assertNotSent(); |
|
190
|
5 |
|
$this->unmarkFailed(); |
|
191
|
5 |
|
$this->sentAt = new DateTimeImmutable; |
|
192
|
5 |
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return DateTimeImmutable|null |
|
196
|
|
|
*/ |
|
197
|
3 |
|
public function failedAt(): ?DateTimeImmutable |
|
198
|
|
|
{ |
|
199
|
3 |
|
return $this->failedAt; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return null|string |
|
204
|
|
|
*/ |
|
205
|
2 |
|
public function failedFor(): ?string |
|
206
|
|
|
{ |
|
207
|
2 |
|
return $this->failedFor; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return bool |
|
212
|
|
|
*/ |
|
213
|
3 |
|
public function isFailed(): bool |
|
214
|
|
|
{ |
|
215
|
3 |
|
return !is_null($this->failedAt()); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $reason |
|
220
|
|
|
*/ |
|
221
|
3 |
|
public function markFailed(string $reason): void |
|
222
|
|
|
{ |
|
223
|
3 |
|
$this->assertNotSent(); |
|
224
|
3 |
|
$this->failedAt = new DateTimeImmutable; |
|
225
|
3 |
|
$this->failedFor = $reason; |
|
226
|
3 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
5 |
|
public function unmarkFailed(): void |
|
232
|
|
|
{ |
|
233
|
5 |
|
$this->failedAt = null; |
|
234
|
5 |
|
$this->failedFor = null; |
|
235
|
5 |
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return DateTimeImmutable|null |
|
239
|
|
|
*/ |
|
240
|
3 |
|
public function lockedAt(): ?DateTimeImmutable |
|
241
|
|
|
{ |
|
242
|
3 |
|
return $this->lockedAt; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @return bool |
|
247
|
|
|
*/ |
|
248
|
3 |
|
public function isLocked(): bool |
|
249
|
|
|
{ |
|
250
|
3 |
|
return !is_null($this->lockedAt()); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
2 |
|
public function lock(): void |
|
254
|
|
|
{ |
|
255
|
2 |
|
$this->lockedAt = new DateTimeImmutable; |
|
256
|
2 |
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @return void |
|
260
|
|
|
*/ |
|
261
|
1 |
|
public function unlock(): void |
|
262
|
|
|
{ |
|
263
|
1 |
|
$this->lockedAt = null; |
|
264
|
1 |
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @return bool |
|
268
|
|
|
*/ |
|
269
|
1 |
|
public function isFresh(): bool |
|
270
|
|
|
{ |
|
271
|
1 |
|
return !$this->isLocked() && !$this->isFailed() && !$this->isSent(); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return void |
|
278
|
|
|
*/ |
|
279
|
7 |
|
private function assertNotSent(): void |
|
280
|
|
|
{ |
|
281
|
7 |
|
if ($this->isSent()) { |
|
282
|
|
|
throw new LogicException( |
|
283
|
|
|
sprintf( |
|
284
|
|
|
'Notification is already sent: "%s" created at %s', |
|
285
|
|
|
$this->subject()->subject(), |
|
286
|
|
|
$this->createdAt()->format(DateTime::W3C) |
|
287
|
|
|
) |
|
288
|
|
|
); |
|
289
|
|
|
} |
|
290
|
7 |
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* For InMemoryNotificationRepository only. |
|
294
|
|
|
* |
|
295
|
|
|
* @param NotificationId $notificationId |
|
296
|
|
|
*/ |
|
297
|
2 |
|
public function setNotificationId(NotificationId $notificationId): void |
|
298
|
|
|
{ |
|
299
|
2 |
|
$this->notificationId = $notificationId; |
|
300
|
2 |
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.