Passed
Push — master ( 6a2ed6...6afdcd )
by Hirofumi
02:31
created

Notification::failedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 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
0 ignored issues
show
Bug introduced by
There is no parameter named $templateName. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     * @param array|null $templateVariables
0 ignored issues
show
Bug introduced by
There is no parameter named $templateVariables. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

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