|
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, |
|
77
|
|
|
array $orderings = null, |
|
78
|
|
|
int $maxResults = null, |
|
79
|
|
|
int $firstResult = null |
|
80
|
|
|
): array { |
|
81
|
|
|
$notifications = |
|
82
|
2 |
|
array_values( |
|
83
|
2 |
|
array_filter( |
|
84
|
2 |
|
$this->notifications, |
|
85
|
|
|
function (Notification $notification) use ($specification) { |
|
86
|
2 |
|
return $specification->isSatisfiedBy($notification); |
|
87
|
2 |
|
} |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
// No max results or first result implementation |
|
92
|
|
|
|
|
93
|
2 |
|
return $notifications; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return NotificationId |
|
98
|
|
|
*/ |
|
99
|
4 |
|
private function nextIdentity(): NotificationId |
|
100
|
|
|
{ |
|
101
|
4 |
|
return new NotificationId($this->nextIdentity++); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|