Passed
Push — master ( 66351d...335aef )
by Hirofumi
20:01
created

DoctrineNotificationRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 76
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

6 Methods

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