Test Failed
Push — master ( 6f7a22...abd8b8 )
by Hirofumi
31:24
created

Notification::failedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
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 8
     */
65
    protected $failedFor;
66
67
    /**
68
     * @param Destination $destination
69
     * @param Subject $subject
70
     * @param Body $body
71
     * @param DeduplicationKey|null $deduplicationKey
72 8
     * @param string|null $templateName
73 8
     * @param array|null $templateVariables
74 8
     */
75 8
    public function __construct(
76 8
        Destination $destination,
77 8
        Subject $subject,
78 8
        Body $body,
79 8
        DeduplicationKey $deduplicationKey = null,
80 8
        string $templateName = null,
81
        array $templateVariables = null
82
    ) {
83
        $this->destination = $destination;
84
        $this->subject = $subject;
85 1
        $this->body = $body;
86
        $this->deduplicationKey = $deduplicationKey;
87 1
        $this->templateName = $templateName;
88
        $this->templateVariables = $templateVariables;
89
        $this->createdAt = new DateTimeImmutable;
90
        $this->sentAt = null;
91
        $this->failedAt = null;
92
        $this->failedFor = null;
93 4
    }
94
95 4
    /**
96
     * @return null|NotificationId
97
     */
98
    public function notificationId(): ?NotificationId
99
    {
100
        return $this->notificationId;
101 3
    }
102
103 3
    /**
104
     * @return Destination
105
     */
106
    public function destination(): Destination
107
    {
108
        return $this->destination;
109 3
    }
110
111 3
    /**
112
     * @return Subject
113
     */
114
    public function subject(): Subject
115
    {
116
        return $this->subject;
117 4
    }
118
119 4
    /**
120
     * @return Body
121
     */
122
    public function body(): Body
123
    {
124
        return $this->body;
125 1
    }
126
127 1
    /**
128
     * @return null|DeduplicationKey
129
     */
130
    public function deduplicationKey(): ?DeduplicationKey
131
    {
132
        return $this->deduplicationKey;
133 2
    }
134
135 2
    /**
136
     * @return DateTimeImmutable
137
     */
138
    public function createdAt(): DateTimeImmutable
139
    {
140
        return $this->createdAt;
141 2
    }
142
143 2
    /**
144
     * @return DateTimeImmutable|null
145
     */
146
    public function sentAt(): ?DateTimeImmutable
147
    {
148
        return $this->sentAt;
149 2
    }
150
151 2
    /**
152
     * @return bool
153
     */
154 2
    public function isSent(): bool
155 2
    {
156
        return !is_null($this->sentAt());
157
    }
158
159
    /**
160
     * @return void
161
     */
162
    public function markSent(): void
163
    {
164
        $this->assertNotSent();
165
        $this->unmarkFailed();
166
        $this->sentAt = new DateTimeImmutable;
167
    }
168
169
    /**
170
     * @return DateTimeImmutable|null
171
     */
172
    public function failedAt(): ?DateTimeImmutable
173
    {
174
        return $this->failedAt;
175
    }
176
177
    /**
178
     * @return null|string
179
     */
180
    public function failedFor(): ?string
181
    {
182
        return $this->failedFor;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function isFailed(): bool
189
    {
190
        return !is_null($this->failedAt());
191
    }
192
193
    /**
194
     * @return void
195
     */
196
    public function markFailed(string $reason): void
197
    {
198
        $this->assertNotSent();
199
        $this->failedAt = new DateTimeImmutable;
200
        $this->failedFor = $reason;
201
    }
202
203
    /**
204
     * @return void
205
     */
206
    public function unmarkFailed(): void
207
    {
208
        $this->failedAt = null;
209
        $this->failedFor = null;
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function isFresh(): bool
216
    {
217
        return !$this->isSent() && !$this->isFailed();
218
    }
219
220
    /**
221
     * @return void
222
     */
223
    private function assertNotSent(): void
224
    {
225
        if ($this->isSent()) {
226
            throw new LogicException(
227
                sprintf(
228
                    'Notification is already sent: "%s" created at %s',
229
                    $this->subject(),
230
                    $this->createdAt()->format(DateTime::W3C)
231
                )
232
            );
233
        }
234
    }
235
}
236
237