Passed
Push — master ( 47d367...c6d1df )
by Hirofumi
06:59
created

DoctrineNotificationRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 75
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 13 3
A persist() 0 7 2
A notificationOfId() 0 4 1
A hasNotificationOfDeduplicationKey() 0 4 1
A freshNotifications() 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 Doctrine\ORM\Mapping;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\OptimisticLockException;
10
use Doctrine\ORM\ORMException;
11
use LogicException;
12
use Shippinno\Notification\Domain\Model\DeduplicationKey;
13
use Shippinno\Notification\Domain\Model\Notification;
14
use Shippinno\Notification\Domain\Model\NotificationId;
15
use Shippinno\Notification\Domain\Model\NotificationRepository;
16
17
class DoctrineNotificationRepository extends EntityRepository implements NotificationRepository
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $isPrecocious;
23
24
    /**
25
     * {@inheritdoc}
26
     * @param bool $isPrecocious
27
     */
28 3
    public function __construct($em, ClassMetadata $class, bool $isPrecocious)
29
    {
30 3
        $this->isPrecocious = $isPrecocious;
31 3
        parent::__construct($em, $class);
32 3
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 3
    public function add(Notification $notification, bool $precocious = false): void
38
    {
39 3
        $deduplicationKey = $notification->deduplicationKey();
40 3
        if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) {
41 1
            throw new LogicException(
42 1
                sprintf(
43 1
                    'Notification of deduplication key (%s) already exists.',
44 1
                    $deduplicationKey
45
                )
46
            );
47
        }
48 3
        $this->persist($notification);
49 3
    }
50
51
    /**
52
     * {@inheritdoc}
53
     * @throws ORMException
54
     * @throws OptimisticLockException
55
     */
56 3
    public function persist(Notification $notification): void
57
    {
58 3
        $this->getEntityManager()->persist($notification);
59 3
        if ($this->isPrecocious) {
60 3
            $this->getEntityManager()->flush();
61
        }
62 3
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function notificationOfId(NotificationId $notificationId): ?Notification
68
    {
69
        return $this->find($notificationId);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool
76
    {
77 2
        return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey]));
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2
    public function freshNotifications(): array
84
    {
85 2
        return $this->createQueryBuilder('n')
86 2
            ->where('n.sentAt IS NULL')
87 2
            ->orderBy('n.notificationId', 'ASC')
88 2
            ->getQuery()
89 2
            ->getResult();
90
    }
91
}
92