Passed
Push — master ( e1e064...6a8420 )
by Hirofumi
02:56
created

Notification::metadata()   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
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 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 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 15
    public function __construct(
77
        Destination $destination,
78
        Subject $subject,
79
        Body $body,
80
        DeduplicationKey $deduplicationKey = null,
81
        array $metadata = null
82
    ) {
83 15
        $this->destination = $destination;
84 15
        $this->subject = $subject;
85 15
        $this->body = $body;
86 15
        $this->deduplicationKey = $deduplicationKey;
87 15
        $this->metadata = $metadata;
88 15
        $this->createdAt = new DateTimeImmutable;
89 15
        $this->sentAt = null;
90 15
        $this->failedAt = null;
91 15
        $this->failedFor = null;
92 15
        $this->lockedAt = null;
93 15
    }
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 11
    public function destination(): Destination
107
    {
108 11
        return $this->destination;
109
    }
110
111
    /**
112
     * @return Subject
113
     */
114 6
    public function subject(): Subject
115
    {
116 6
        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 array|null
137
     */
138
    public function metadata(): ?array
139
    {
140
        return $this->metadata;
141
    }
142
143
    /**
144
     * @param array $metadata
145
     */
146
    public function setMetadata(array $metadata): void
147
    {
148
        $this->metadata = $metadata;
149
    }
150
151
    /**
152
     * @return DateTimeImmutable
153
     */
154 3
    public function createdAt(): DateTimeImmutable
155
    {
156 3
        return $this->createdAt;
157
    }
158
159
    /**
160
     * @return DateTimeImmutable|null
161
     */
162 7
    public function sentAt(): ?DateTimeImmutable
163
    {
164 7
        return $this->sentAt;
165
    }
166
167
    /**
168
     * @return bool
169
     */
170 7
    public function isSent(): bool
171
    {
172 7
        return !is_null($this->sentAt());
173
    }
174
175
    /**
176
     * @return void
177
     */
178 5
    public function markSent(): void
179
    {
180 5
        $this->assertNotSent();
181 5
        $this->unmarkFailed();
182 5
        $this->sentAt = new DateTimeImmutable;
183 5
    }
184
185
    /**
186
     * @return DateTimeImmutable|null
187
     */
188 3
    public function failedAt(): ?DateTimeImmutable
189
    {
190 3
        return $this->failedAt;
191
    }
192
193
    /**
194
     * @return null|string
195
     */
196 2
    public function failedFor(): ?string
197
    {
198 2
        return $this->failedFor;
199
    }
200
201
    /**
202
     * @return bool
203
     */
204 3
    public function isFailed(): bool
205
    {
206 3
        return !is_null($this->failedAt());
207
    }
208
209
    /**
210
     * @param string $reason
211
     */
212 3
    public function markFailed(string $reason): void
213
    {
214 3
        $this->assertNotSent();
215 3
        $this->failedAt = new DateTimeImmutable;
216 3
        $this->failedFor = $reason;
217 3
    }
218
219
    /**
220
     * @return void
221
     */
222 5
    public function unmarkFailed(): void
223
    {
224 5
        $this->failedAt = null;
225 5
        $this->failedFor = null;
226 5
    }
227
228
    /**
229
     * @return DateTimeImmutable|null
230
     */
231 3
    public function lockedAt(): ?DateTimeImmutable
232
    {
233 3
        return $this->lockedAt;
234
    }
235
236
    /**
237
     * @return bool
238
     */
239 3
    public function isLocked(): bool
240
    {
241 3
        return !is_null($this->lockedAt());
242
    }
243
244 2
    public function lock(): void
245
    {
246 2
        $this->lockedAt = new DateTimeImmutable;
247 2
    }
248
249
    /**
250
     * @return void
251
     */
252 1
    public function unlock(): void
253
    {
254 1
        $this->lockedAt = null;
255 1
    }
256
257
    /**
258
     * @return bool
259
     */
260 1
    public function isFresh(): bool
261
    {
262 1
        return !$this->isLocked() && !$this->isFailed() &&  !$this->isSent();
263
    }
264
265
266
267
    /**
268
     * @return void
269
     */
270 7
    private function assertNotSent(): void
271
    {
272 7
        if ($this->isSent()) {
273
            throw new LogicException(
274
                sprintf(
275
                    'Notification is already sent: "%s" created at %s',
276
                    $this->subject()->subject(),
277
                    $this->createdAt()->format(DateTime::W3C)
278
                )
279
            );
280
        }
281 7
    }
282
283
    /**
284
     * For InMemoryNotificationRepository only.
285
     *
286
     * @param NotificationId $notificationId
287
     */
288 2
    public function setNotificationId(NotificationId $notificationId): void
289
    {
290 2
        $this->notificationId = $notificationId;
291 2
    }
292
}
293
294