Passed
Push — master ( 50509a...5c86f2 )
by Hirofumi
03:29
created

DoctrineNotificationRepository::notificationOfId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Infrastructure\Domain\Model;
5
6
use Doctrine\ORM\EntityRepository;
7
use LogicException;
8
use Shippinno\Notification\Domain\Model\DeduplicationKey;
9
use Shippinno\Notification\Domain\Model\Notification;
10
use Shippinno\Notification\Domain\Model\NotificationId;
11
use Shippinno\Notification\Domain\Model\NotificationRepository;
12
13
class DoctrineNotificationRepository extends EntityRepository implements NotificationRepository
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 3
    public function add(Notification $notification): void
19
    {
20 3
        $deduplicationKey = $notification->deduplicationKey();
21 3
        if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) {
22 1
            throw new LogicException(
23 1
                sprintf(
24 1
                    'Notification of deduplication key (%s) already exists.',
25 1
                    $deduplicationKey
26
                )
27
            );
28
        }
29 3
        $this->getEntityManager()->persist($notification);
30 3
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool
36
    {
37 2
        return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey]));
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 2
    public function unsentNotifications(): array
44
    {
45 2
        return $this->createQueryBuilder('n')
46 2
            ->where('n.sentAt IS NULL')
47 2
            ->orderBy('n.notificationId', 'ASC')
48 2
            ->getQuery()
49 2
            ->getResult();
50
    }
51
}
52