|
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\NotificationDeduplicationKeySpecification; |
|
9
|
|
|
use Shippinno\Notification\Domain\Model\NotificationId; |
|
10
|
|
|
use Shippinno\Notification\Domain\Model\NotificationRepository; |
|
11
|
|
|
use Tanigami\Specification\Specification; |
|
12
|
|
|
|
|
13
|
|
|
class InMemoryNotificationRepository implements NotificationRepository |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Notification[] |
|
17
|
|
|
*/ |
|
18
|
|
|
private $notifications = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $nextIdentity = 1; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function add(Notification $notification): void |
|
29
|
|
|
{ |
|
30
|
4 |
|
$deduplicationKey = $notification->deduplicationKey(); |
|
31
|
4 |
|
if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
4 |
|
$notification->setNotificationId($this->nextIdentity()); |
|
35
|
4 |
|
$this->persist($notification); |
|
36
|
4 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
4 |
|
public function persist(Notification $notification): void |
|
42
|
|
|
{ |
|
43
|
4 |
|
$this->notifications[$notification->notificationId()->id()] = $notification; |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public function notificationOfId(NotificationId $notificationId): ?Notification |
|
50
|
|
|
{ |
|
51
|
5 |
|
if (!isset($this->notifications[$notificationId->id()])) { |
|
52
|
2 |
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return clone $this->notifications[$notificationId->id()]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool |
|
62
|
|
|
{ |
|
63
|
1 |
|
foreach ($this->notifications as $notification) { |
|
64
|
1 |
|
if ((new NotificationDeduplicationKeySpecification($deduplicationKey))->isSatisfiedBy($notification)) { |
|
65
|
1 |
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function query( |
|
76
|
|
|
Specification $specification = null, |
|
77
|
|
|
array $orderings = null, |
|
78
|
|
|
int $maxResults = null, |
|
79
|
|
|
int $firstResult = null |
|
80
|
|
|
): array { |
|
81
|
2 |
|
$notifications = $this->notifications; |
|
82
|
2 |
|
if (!is_null($specification)) { |
|
83
|
|
|
$notifications = |
|
84
|
2 |
|
array_values( |
|
85
|
2 |
|
array_filter( |
|
86
|
2 |
|
$this->notifications, |
|
87
|
|
|
function (Notification $notification) use ($specification) { |
|
88
|
2 |
|
return $specification->isSatisfiedBy($notification); |
|
89
|
2 |
|
} |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// No max results or first result implementation |
|
95
|
|
|
|
|
96
|
2 |
|
return $notifications; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return NotificationId |
|
101
|
|
|
*/ |
|
102
|
4 |
|
private function nextIdentity(): NotificationId |
|
103
|
|
|
{ |
|
104
|
4 |
|
return new NotificationId($this->nextIdentity++); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function remove(Notification $notification): void |
|
111
|
|
|
{ |
|
112
|
|
|
unset($this->notifications[$notification->notificationId()->id()]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function removeAll(array $notifications): void |
|
119
|
|
|
{ |
|
120
|
|
|
foreach ($notifications as $notification) { |
|
121
|
|
|
$this->remove($notification); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|