1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shippinno\Notification\Domain\Model; |
5
|
|
|
|
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use LogicException; |
8
|
|
|
|
9
|
|
|
class Notification |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var null|NotificationId |
13
|
|
|
*/ |
14
|
|
|
protected $notificationId; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Destination |
18
|
|
|
*/ |
19
|
|
|
protected $destination; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Subject |
23
|
|
|
*/ |
24
|
|
|
protected $subject; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Body |
28
|
|
|
*/ |
29
|
|
|
protected $body; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var null|DeduplicationKey |
33
|
|
|
*/ |
34
|
|
|
protected $deduplicationKey; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var null|string |
38
|
|
|
*/ |
39
|
|
|
protected $templateName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array|null |
43
|
|
|
*/ |
44
|
|
|
protected $templateVariables; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var DateTimeImmutable |
48
|
|
|
*/ |
49
|
|
|
protected $createdAt; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var null|DateTimeImmutable |
53
|
|
|
*/ |
54
|
|
|
protected $sentAt; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Destination $destination |
58
|
|
|
* @param Subject $subject |
59
|
|
|
* @param Body $body |
60
|
|
|
* @param DeduplicationKey|null $deduplicationKey |
61
|
|
|
* @param string|null $templateName |
62
|
|
|
* @param array|null $templateVariables |
63
|
|
|
*/ |
64
|
8 |
|
public function __construct( |
65
|
|
|
Destination $destination, |
66
|
|
|
Subject $subject, |
67
|
|
|
Body $body, |
68
|
|
|
DeduplicationKey $deduplicationKey = null, |
69
|
|
|
string $templateName = null, |
70
|
|
|
array $templateVariables = null |
71
|
|
|
) { |
72
|
8 |
|
$this->destination = $destination; |
73
|
8 |
|
$this->subject = $subject; |
74
|
8 |
|
$this->body = $body; |
75
|
8 |
|
$this->deduplicationKey = $deduplicationKey; |
76
|
8 |
|
$this->templateName = $templateName; |
77
|
8 |
|
$this->templateVariables = $templateVariables; |
78
|
8 |
|
$this->createdAt = new DateTimeImmutable; |
79
|
8 |
|
$this->sentAt = null; |
80
|
8 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return null|NotificationId |
84
|
|
|
*/ |
85
|
1 |
|
public function notificationId(): ?NotificationId |
86
|
|
|
{ |
87
|
1 |
|
return $this->notificationId; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Destination |
92
|
|
|
*/ |
93
|
4 |
|
public function destination(): Destination |
94
|
|
|
{ |
95
|
4 |
|
return $this->destination; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Subject |
100
|
|
|
*/ |
101
|
3 |
|
public function subject(): Subject |
102
|
|
|
{ |
103
|
3 |
|
return $this->subject; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Body |
108
|
|
|
*/ |
109
|
3 |
|
public function body(): Body |
110
|
|
|
{ |
111
|
3 |
|
return $this->body; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return null|DeduplicationKey |
116
|
|
|
*/ |
117
|
4 |
|
public function deduplicationKey(): ?DeduplicationKey |
118
|
|
|
{ |
119
|
4 |
|
return $this->deduplicationKey; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return DateTimeImmutable |
124
|
|
|
*/ |
125
|
1 |
|
public function createdAt(): DateTimeImmutable |
126
|
|
|
{ |
127
|
1 |
|
return $this->createdAt; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return DateTimeImmutable|null |
132
|
|
|
*/ |
133
|
2 |
|
public function sentAt(): ?DateTimeImmutable |
134
|
|
|
{ |
135
|
2 |
|
return $this->sentAt; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
2 |
|
public function isSent(): bool |
142
|
|
|
{ |
143
|
2 |
|
return !is_null($this->sentAt()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
2 |
|
public function markSent(): void |
150
|
|
|
{ |
151
|
2 |
|
if ($this->isSent()) { |
152
|
|
|
throw new LogicException(sprintf('Notification is already sent: %s', $this->notificationId->id())); |
153
|
|
|
} |
154
|
2 |
|
$this->sentAt = new DateTimeImmutable; |
155
|
2 |
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|