Passed
Push — master ( 79f40f...79f40f )
by Hirofumi
55s queued 10s
created

DoctrineNotificationRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\OptimisticLockException;
9
use Doctrine\ORM\ORMException;
10
use LogicException;
11
use Shippinno\Notification\Domain\Model\DeduplicationException;
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 DeduplicationException($deduplicationKey);
42
        }
43 3
        $this->persist($notification);
44 3
    }
45
46
    /**
47
     * {@inheritdoc}
48
     * @throws ORMException
49
     * @throws OptimisticLockException
50
     */
51 3
    public function persist(Notification $notification): void
52
    {
53 3
        $this->getEntityManager()->persist($notification);
54 3
        if ($this->isPrecocious) {
55 3
            $this->getEntityManager()->flush();
56
        }
57 3
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function notificationOfId(NotificationId $notificationId): ?Notification
63
    {
64
        return $this->find($notificationId);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool
71
    {
72 2
        return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey]));
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function freshNotifications(): array
79
    {
80 2
        return $this->createQueryBuilder('n')
81 2
            ->where('n.sentAt IS NULL')
82 2
            ->andWhere('n.failedAt IS NULL')
83 2
            ->orderBy('n.notificationId', 'ASC')
84 2
            ->getQuery()
85 2
            ->getResult();
86
    }
87
}
88