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 null|string |
39
|
|
|
*/ |
40
|
|
|
protected $templateName; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array|null |
44
|
|
|
*/ |
45
|
|
|
protected $templateVariables; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var DateTimeImmutable |
49
|
|
|
*/ |
50
|
|
|
protected $createdAt; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var null|DateTimeImmutable |
54
|
|
|
*/ |
55
|
|
|
protected $sentAt; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var null|DateTimeImmutable |
59
|
|
|
*/ |
60
|
|
|
protected $failedAt; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var null|string |
64
|
|
|
*/ |
65
|
|
|
protected $failedFor; |
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
|
|
|
*/ |
75
|
13 |
|
public function __construct( |
76
|
|
|
Destination $destination, |
77
|
|
|
Subject $subject, |
78
|
|
|
Body $body, |
79
|
|
|
DeduplicationKey $deduplicationKey = null, |
80
|
|
|
string $templateName = null, |
81
|
|
|
array $templateVariables = null |
82
|
|
|
) { |
83
|
13 |
|
$this->destination = $destination; |
84
|
13 |
|
$this->subject = $subject; |
85
|
13 |
|
$this->body = $body; |
86
|
13 |
|
$this->deduplicationKey = $deduplicationKey; |
87
|
13 |
|
$this->templateName = $templateName; |
88
|
13 |
|
$this->templateVariables = $templateVariables; |
89
|
13 |
|
$this->createdAt = new DateTimeImmutable; |
90
|
13 |
|
$this->sentAt = null; |
91
|
13 |
|
$this->failedAt = null; |
92
|
13 |
|
$this->failedFor = null; |
93
|
13 |
|
} |
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
|
9 |
|
public function destination(): Destination |
107
|
|
|
{ |
108
|
9 |
|
return $this->destination; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Subject |
113
|
|
|
*/ |
114
|
5 |
|
public function subject(): Subject |
115
|
|
|
{ |
116
|
5 |
|
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 DateTimeImmutable |
137
|
|
|
*/ |
138
|
2 |
|
public function createdAt(): DateTimeImmutable |
139
|
|
|
{ |
140
|
2 |
|
return $this->createdAt; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return DateTimeImmutable|null |
145
|
|
|
*/ |
146
|
6 |
|
public function sentAt(): ?DateTimeImmutable |
147
|
|
|
{ |
148
|
6 |
|
return $this->sentAt; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
6 |
|
public function isSent(): bool |
155
|
|
|
{ |
156
|
6 |
|
return !is_null($this->sentAt()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
4 |
|
public function markSent(): void |
163
|
|
|
{ |
164
|
4 |
|
$this->assertNotSent(); |
165
|
4 |
|
$this->unmarkFailed(); |
166
|
4 |
|
$this->sentAt = new DateTimeImmutable; |
167
|
4 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return DateTimeImmutable|null |
171
|
|
|
*/ |
172
|
3 |
|
public function failedAt(): ?DateTimeImmutable |
173
|
|
|
{ |
174
|
3 |
|
return $this->failedAt; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return null|string |
179
|
|
|
*/ |
180
|
2 |
|
public function failedFor(): ?string |
181
|
|
|
{ |
182
|
2 |
|
return $this->failedFor; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
3 |
|
public function isFailed(): bool |
189
|
|
|
{ |
190
|
3 |
|
return !is_null($this->failedAt()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
3 |
|
public function markFailed(string $reason): void |
197
|
|
|
{ |
198
|
3 |
|
$this->assertNotSent(); |
199
|
3 |
|
$this->failedAt = new DateTimeImmutable; |
200
|
3 |
|
$this->failedFor = $reason; |
201
|
3 |
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
4 |
|
public function unmarkFailed(): void |
207
|
|
|
{ |
208
|
4 |
|
$this->failedAt = null; |
209
|
4 |
|
$this->failedFor = null; |
210
|
4 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
1 |
|
public function isFresh(): bool |
216
|
|
|
{ |
217
|
1 |
|
return !$this->isSent() && !$this->isFailed(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return void |
222
|
|
|
*/ |
223
|
6 |
|
private function assertNotSent(): void |
224
|
|
|
{ |
225
|
6 |
|
if ($this->isSent()) { |
226
|
|
|
throw new LogicException( |
227
|
|
|
sprintf( |
228
|
|
|
'Notification is already sent: "%s" created at %s', |
229
|
|
|
$this->subject()->subject(), |
230
|
|
|
$this->createdAt()->format(DateTime::W3C) |
231
|
|
|
) |
232
|
|
|
); |
233
|
|
|
} |
234
|
6 |
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* For InMemoryNotificationRepository only. |
238
|
|
|
* |
239
|
|
|
* @param NotificationId $notificationId |
240
|
|
|
*/ |
241
|
2 |
|
public function setNotificationId(NotificationId $notificationId): void |
242
|
|
|
{ |
243
|
2 |
|
$this->notificationId = $notificationId; |
244
|
2 |
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|