Passed
Push — master ( e57bba...803dec )
by Hirofumi
05:19
created

DoctrineNotificationRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 72
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 8 3
A persist() 0 7 2
A notificationOfId() 0 4 1
A hasNotificationOfDeduplicationKey() 0 4 1
A freshNotifications() 0 10 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 Shippinno\Notification\Domain\Model\DeduplicationException;
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 DeduplicationException($deduplicationKey);
41
        }
42 3
        $this->persist($notification);
43 3
    }
44
45
    /**
46
     * {@inheritdoc}
47
     * @throws ORMException
48
     * @throws OptimisticLockException
49
     */
50 3
    public function persist(Notification $notification): void
51
    {
52 3
        $this->getEntityManager()->persist($notification);
53 3
        if ($this->isPrecocious) {
54 3
            $this->getEntityManager()->flush();
55
        }
56 3
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function notificationOfId(NotificationId $notificationId): ?Notification
62
    {
63
        return $this->find($notificationId);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 2
    public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool
70
    {
71 2
        return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey]));
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function freshNotifications(): array
78
    {
79 2
        return $this->createQueryBuilder('n')
80 2
            ->where('n.sentAt IS NULL')
81 2
            ->andWhere('n.failedAt IS NULL')
82 2
            ->andWhere('n.lockedAt IS NULL')
83 2
            ->orderBy('n.notificationId', 'ASC')
84 2
            ->getQuery()
85 2
            ->getResult();
86
    }
87
}
88