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

DoctrineNotificationRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 13 3
A hasNotificationOfDeduplicationKey() 0 4 1
A unsentNotifications() 0 8 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