1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shippinno\Notification\Infrastructure\Domain\Model; |
5
|
|
|
|
6
|
|
|
use Shippinno\Notification\Domain\Model\DeduplicationKey; |
7
|
|
|
use Shippinno\Notification\Domain\Model\Notification; |
8
|
|
|
use Shippinno\Notification\Domain\Model\NotificationId; |
9
|
|
|
use Shippinno\Notification\Domain\Model\NotificationRepository; |
10
|
|
|
|
11
|
|
|
class InMemoryNotificationRepository implements NotificationRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Notification[] |
15
|
|
|
*/ |
16
|
|
|
private $notifications = []; |
17
|
|
|
|
18
|
|
|
private $nextIdentity = 1; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
1 |
|
public function add(Notification $notification): void |
24
|
|
|
{ |
25
|
1 |
|
$deduplicationKey = $notification->deduplicationKey(); |
26
|
1 |
|
if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
1 |
|
$this->setNotification($notification); |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
2 |
|
public function markSent(Notification $notification): void |
36
|
|
|
{ |
37
|
2 |
|
$notification->markSent(); |
38
|
1 |
|
$this->setNotification($notification); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function markFailed(Notification $notification, string $reason): void |
45
|
|
|
{ |
46
|
|
|
$notification->markFailed($reason); |
47
|
|
|
$this->setNotification($notification); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Notification $notification |
52
|
|
|
*/ |
53
|
|
|
private function setNotification(Notification $notification): void |
54
|
|
|
{ |
55
|
|
|
$this->notifications[$this->nextIdentity()->id()] = $notification; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function notificationOfId(NotificationId $notificationId): ?Notification |
63
|
|
|
{ |
64
|
|
|
if (!isset($this->notifications[$notificationId->id()])) { |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->notifications[$notificationId->id()]; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool |
75
|
|
|
{ |
76
|
|
|
foreach ($this->notifications as $notification) { |
77
|
|
|
if ($notification->deduplicationKey()->equals($deduplicationKey)) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function unsentNotifications(): array |
87
|
|
|
{ |
88
|
|
|
return array_filter($this->notifications, function (Notification $notification) { |
89
|
|
|
return !$notification->isSent(); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return NotificationId |
95
|
|
|
*/ |
96
|
|
|
private function nextIdentity(): NotificationId |
97
|
|
|
{ |
98
|
|
|
return new NotificationId($this->nextIdentity++); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|