Passed
Push — master ( 79f40f...37a828 )
by Hirofumi
02:31
created

Notification   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.06%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 0
dl 0
loc 271
ccs 67
cts 72
cp 0.9306
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A notificationId() 0 4 1
A destination() 0 4 1
A subject() 0 4 1
A body() 0 4 1
A deduplicationKey() 0 4 1
A createdAt() 0 4 1
A sentAt() 0 4 1
A isSent() 0 4 1
A markSent() 0 6 1
A failedAt() 0 4 1
A failedFor() 0 4 1
A isFailed() 0 4 1
A markFailed() 0 6 1
A unmarkFailed() 0 5 1
A lockedAt() 0 4 1
A isLocked() 0 4 1
A lock() 0 4 1
A unlock() 0 4 1
A isFresh() 0 4 3
A assertNotSent() 0 12 2
A setNotificationId() 0 4 1
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
     * @var null|DateTimeImmutable
69
     */
70
    protected $lockedAt;
71
72
    /**
73
     * @param Destination $destination
74
     * @param Subject $subject
75
     * @param Body $body
76
     * @param DeduplicationKey|null $deduplicationKey
77
     * @param string|null $templateName
78
     * @param array|null $templateVariables
79
     */
80 15
    public function __construct(
81
        Destination $destination,
82
        Subject $subject,
83
        Body $body,
84
        DeduplicationKey $deduplicationKey = null,
85
        string $templateName = null,
86
        array $templateVariables = null
87
    ) {
88 15
        $this->destination = $destination;
89 15
        $this->subject = $subject;
90 15
        $this->body = $body;
91 15
        $this->deduplicationKey = $deduplicationKey;
92 15
        $this->templateName = $templateName;
93 15
        $this->templateVariables = $templateVariables;
94 15
        $this->createdAt = new DateTimeImmutable;
95 15
        $this->sentAt = null;
96 15
        $this->failedAt = null;
97 15
        $this->failedFor = null;
98 15
        $this->lockedAt = null;
99 15
    }
100
101
    /**
102
     * @return null|NotificationId
103
     */
104 3
    public function notificationId(): ?NotificationId
105
    {
106 3
        return $this->notificationId;
107
    }
108
109
    /**
110
     * @return Destination
111
     */
112 11
    public function destination(): Destination
113
    {
114 11
        return $this->destination;
115
    }
116
117
    /**
118
     * @return Subject
119
     */
120 6
    public function subject(): Subject
121
    {
122 6
        return $this->subject;
123
    }
124
125
    /**
126
     * @return Body
127
     */
128 4
    public function body(): Body
129
    {
130 4
        return $this->body;
131
    }
132
133
    /**
134
     * @return null|DeduplicationKey
135
     */
136 5
    public function deduplicationKey(): ?DeduplicationKey
137
    {
138 5
        return $this->deduplicationKey;
139
    }
140
141
    /**
142
     * @return DateTimeImmutable
143
     */
144 3
    public function createdAt(): DateTimeImmutable
145
    {
146 3
        return $this->createdAt;
147
    }
148
149
    /**
150
     * @return DateTimeImmutable|null
151
     */
152 7
    public function sentAt(): ?DateTimeImmutable
153
    {
154 7
        return $this->sentAt;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160 7
    public function isSent(): bool
161
    {
162 7
        return !is_null($this->sentAt());
163
    }
164
165
    /**
166
     * @return void
167
     */
168 5
    public function markSent(): void
169
    {
170 5
        $this->assertNotSent();
171 5
        $this->unmarkFailed();
172 5
        $this->sentAt = new DateTimeImmutable;
173 5
    }
174
175
    /**
176
     * @return DateTimeImmutable|null
177
     */
178 3
    public function failedAt(): ?DateTimeImmutable
179
    {
180 3
        return $this->failedAt;
181
    }
182
183
    /**
184
     * @return null|string
185
     */
186 2
    public function failedFor(): ?string
187
    {
188 2
        return $this->failedFor;
189
    }
190
191
    /**
192
     * @return bool
193
     */
194 3
    public function isFailed(): bool
195
    {
196 3
        return !is_null($this->failedAt());
197
    }
198
199
    /**
200
     * @param string $reason
201
     */
202 3
    public function markFailed(string $reason): void
203
    {
204 3
        $this->assertNotSent();
205 3
        $this->failedAt = new DateTimeImmutable;
206 3
        $this->failedFor = $reason;
207 3
    }
208
209
    /**
210
     * @return void
211
     */
212 5
    public function unmarkFailed(): void
213
    {
214 5
        $this->failedAt = null;
215 5
        $this->failedFor = null;
216 5
    }
217
218
    /**
219
     * @return DateTimeImmutable|null
220
     */
221 3
    public function lockedAt(): ?DateTimeImmutable
222
    {
223 3
        return $this->lockedAt;
224
    }
225
226
    /**
227
     * @return bool
228
     */
229 3
    public function isLocked(): bool
230
    {
231 3
        return !is_null($this->lockedAt());
232
    }
233
234 2
    public function lock(): void
235
    {
236 2
        $this->lockedAt = new DateTimeImmutable;
237 2
    }
238
239
    /**
240
     * @return void
241
     */
242 1
    public function unlock(): void
243
    {
244 1
        $this->lockedAt = null;
245 1
    }
246
247
    /**
248
     * @return bool
249
     */
250 1
    public function isFresh(): bool
251
    {
252 1
        return !$this->isLocked() && !$this->isFailed() &&  !$this->isSent();
253
    }
254
255
    /**
256
     * @return void
257
     */
258 7
    private function assertNotSent(): void
259
    {
260 7
        if ($this->isSent()) {
261
            throw new LogicException(
262
                sprintf(
263
                    'Notification is already sent: "%s" created at %s',
264
                    $this->subject()->subject(),
265
                    $this->createdAt()->format(DateTime::W3C)
266
                )
267
            );
268
        }
269 7
    }
270
271
    /**
272
     * For InMemoryNotificationRepository only.
273
     *
274
     * @param NotificationId $notificationId
275
     */
276 2
    public function setNotificationId(NotificationId $notificationId): void
277
    {
278 2
        $this->notificationId = $notificationId;
279 2
    }
280
}
281
282