Passed
Push — master ( e57bba...803dec )
by Hirofumi
05:19
created

Notification::matchesMetadataSpecs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use Adbar\Dot;
7
use DateTime;
8
use DateTimeImmutable;
9
use LogicException;
10
11
class Notification
12
{
13
    /**
14
     * @var null|NotificationId
15
     */
16
    protected $notificationId;
17
18
    /**
19
     * @var Destination
20
     */
21
    protected $destination;
22
23
    /**
24
     * @var Subject
25
     */
26
    protected $subject;
27
28
    /**
29
     * @var Body
30
     */
31
    protected $body;
32
33
    /**
34
     * @var null|DeduplicationKey
35
     */
36
    protected $deduplicationKey;
37
38
    /**
39
     * @var array|null
40
     */
41
    protected $metadata;
42
43
    /**
44
     * @var DateTimeImmutable
45
     */
46
    protected $createdAt;
47
48
    /**
49
     * @var null|DateTimeImmutable
50
     */
51
    protected $sentAt;
52
53
    /**
54
     * @var null|DateTimeImmutable
55
     */
56
    protected $failedAt;
57
58
    /**
59
     * @var null|string
60
     */
61
    protected $failedFor;
62
63
    /**
64
     * @var null|DateTimeImmutable
65
     */
66
    protected $lockedAt;
67
68
    /**
69
     * @param Destination $destination
70
     * @param Subject $subject
71
     * @param Body $body
72
     * @param DeduplicationKey|null $deduplicationKey
73
     * @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...
74
     * @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...
75
     * @param array|null $metadata
76
     */
77 16
    public function __construct(
78
        Destination $destination,
79
        Subject $subject,
80
        Body $body,
81
        DeduplicationKey $deduplicationKey = null,
82
        array $metadata = null
83
    ) {
84 16
        $this->destination = $destination;
85 16
        $this->subject = $subject;
86 16
        $this->body = $body;
87 16
        $this->deduplicationKey = $deduplicationKey;
88 16
        $this->metadata = $metadata;
89 16
        $this->createdAt = new DateTimeImmutable;
90 16
        $this->sentAt = null;
91 16
        $this->failedAt = null;
92 16
        $this->failedFor = null;
93 16
        $this->lockedAt = null;
94 16
    }
95
96
    /**
97
     * @return null|NotificationId
98
     */
99 3
    public function notificationId(): ?NotificationId
100
    {
101 3
        return $this->notificationId;
102
    }
103
104
    /**
105
     * @return Destination
106
     */
107 11
    public function destination(): Destination
108
    {
109 11
        return $this->destination;
110
    }
111
112
    /**
113
     * @return Subject
114
     */
115 6
    public function subject(): Subject
116
    {
117 6
        return $this->subject;
118
    }
119
120
    /**
121
     * @return Body
122
     */
123 4
    public function body(): Body
124
    {
125 4
        return $this->body;
126
    }
127
128
    /**
129
     * @return null|DeduplicationKey
130
     */
131 5
    public function deduplicationKey(): ?DeduplicationKey
132
    {
133 5
        return $this->deduplicationKey;
134
    }
135
136
    /**
137
     * @return array|null
138
     */
139
    public function metadata(): ?array
140
    {
141
        return $this->metadata;
142
    }
143
144
    /**
145
     * @param array $metadata
146
     */
147
    public function setMetadata(array $metadata): void
148
    {
149
        $this->metadata = $metadata;
150
    }
151
152
    /**
153
     * @param string $key
154
     * @param mixed $value
155
     */
156
    public function addMetadata(string $key, $value): void
157
    {
158
        $this->metadata[$key] = $value;
159
    }
160
161
    /**
162
     * @param array $metadataSpecs
163
     * @return bool
164
     */
165 2
    public function matchesMetadataSpecs(array $metadataSpecs): bool
166
    {
167 2
        $dotMetadata = new Dot($this->metadata);
168 2
        foreach ($metadataSpecs as $key => $value) {
169 2
            if ($dotMetadata->get($key) !== $value) {
170 2
                return false;
171
            }
172
        }
173
174 2
        return true;
175
    }
176
177
178
    /**
179
     * @return DateTimeImmutable
180
     */
181 3
    public function createdAt(): DateTimeImmutable
182
    {
183 3
        return $this->createdAt;
184
    }
185
186
    /**
187
     * @return DateTimeImmutable|null
188
     */
189 7
    public function sentAt(): ?DateTimeImmutable
190
    {
191 7
        return $this->sentAt;
192
    }
193
194
    /**
195
     * @return bool
196
     */
197 7
    public function isSent(): bool
198
    {
199 7
        return !is_null($this->sentAt());
200
    }
201
202
    /**
203
     * @return void
204
     */
205 5
    public function markSent(): void
206
    {
207 5
        $this->assertNotSent();
208 5
        $this->unmarkFailed();
209 5
        $this->sentAt = new DateTimeImmutable;
210 5
    }
211
212
    /**
213
     * @return DateTimeImmutable|null
214
     */
215 3
    public function failedAt(): ?DateTimeImmutable
216
    {
217 3
        return $this->failedAt;
218
    }
219
220
    /**
221
     * @return null|string
222
     */
223 2
    public function failedFor(): ?string
224
    {
225 2
        return $this->failedFor;
226
    }
227
228
    /**
229
     * @return bool
230
     */
231 3
    public function isFailed(): bool
232
    {
233 3
        return !is_null($this->failedAt());
234
    }
235
236
    /**
237
     * @param string $reason
238
     */
239 3
    public function markFailed(string $reason): void
240
    {
241 3
        $this->assertNotSent();
242 3
        $this->failedAt = new DateTimeImmutable;
243 3
        $this->failedFor = $reason;
244 3
    }
245
246
    /**
247
     * @return void
248
     */
249 5
    public function unmarkFailed(): void
250
    {
251 5
        $this->failedAt = null;
252 5
        $this->failedFor = null;
253 5
    }
254
255
    /**
256
     * @return DateTimeImmutable|null
257
     */
258 3
    public function lockedAt(): ?DateTimeImmutable
259
    {
260 3
        return $this->lockedAt;
261
    }
262
263
    /**
264
     * @return bool
265
     */
266 3
    public function isLocked(): bool
267
    {
268 3
        return !is_null($this->lockedAt());
269
    }
270
271
    /**
272
     * @return void
273
     */
274 2
    public function lock(): void
275
    {
276 2
        $this->lockedAt = new DateTimeImmutable;
277 2
    }
278
279
    /**
280
     * @return void
281
     */
282 1
    public function unlock(): void
283
    {
284 1
        $this->lockedAt = null;
285 1
    }
286
287
    /**
288
     * @return bool
289
     */
290 1
    public function isFresh(): bool
291
    {
292 1
        return !$this->isLocked() && !$this->isFailed() &&  !$this->isSent();
293
    }
294
295
    /**
296
     * @return void
297
     */
298 7
    private function assertNotSent(): void
299
    {
300 7
        if ($this->isSent()) {
301
            throw new LogicException(
302
                sprintf(
303
                    'Notification is already sent: "%s" created at %s',
304
                    $this->subject()->subject(),
305
                    $this->createdAt()->format(DateTime::W3C)
306
                )
307
            );
308
        }
309 7
    }
310
311
    /**
312
     * For InMemoryNotificationRepository only.
313
     *
314
     * @param NotificationId $notificationId
315
     */
316 2
    public function setNotificationId(NotificationId $notificationId): void
317
    {
318 2
        $this->notificationId = $notificationId;
319 2
    }
320
}
321
322