Passed
Push — master ( 6afdcd...dc022a )
by Hirofumi
05:03
created

freshNotifications()   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 0
crap 3
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