Test Failed
Push — master ( dc022a...e203c1 )
by Hirofumi
02:24
created

DoctrineNotificationRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 8
dl 0
loc 100
ccs 32
cts 38
cp 0.8421
rs 10
c 0
b 0
f 0

7 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
B query() 0 24 6
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\NotificationDeduplicationKeySpecification;
14
use Shippinno\Notification\Domain\Model\NotificationId;
15
use Shippinno\Notification\Domain\Model\NotificationRepository;
16
use Tanigami\Specification\Specification;
17
18
class DoctrineNotificationRepository extends EntityRepository implements NotificationRepository
19
{
20
    /**
21
     * @var bool
22
     */
23
    private $isPrecocious;
24
25
    /**
26
     * {@inheritdoc}
27
     * @param bool $isPrecocious
28
     */
29 4
    public function __construct($em, ClassMetadata $class, bool $isPrecocious)
30
    {
31 4
        $this->isPrecocious = $isPrecocious;
32 4
        parent::__construct($em, $class);
33 4
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 4
    public function add(Notification $notification, bool $precocious = false): void
39
    {
40 4
        $deduplicationKey = $notification->deduplicationKey();
41 4
        if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) {
42 1
            throw new DeduplicationException($deduplicationKey);
43
        }
44 4
        $this->persist($notification);
45 4
    }
46
47
    /**
48
     * {@inheritdoc}
49
     * @throws ORMException
50
     * @throws OptimisticLockException
51
     */
52 4
    public function persist(Notification $notification): void
53
    {
54 4
        $this->getEntityManager()->persist($notification);
55 4
        if ($this->isPrecocious) {
56 4
            $this->getEntityManager()->flush();
57
        }
58 4
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function notificationOfId(NotificationId $notificationId): ?Notification
64
    {
65
        return $this->find($notificationId);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool
72
    {
73 2
        return !empty($this->query(new NotificationDeduplicationKeySpecification($deduplicationKey)));
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function freshNotifications(): array
80
    {
81 2
        return $this->createQueryBuilder('n')
82 2
            ->where('n.sentAt IS NULL')
83 2
            ->andWhere('n.failedAt IS NULL')
84 2
            ->andWhere('n.lockedAt IS NULL')
85 2
            ->orderBy('n.notificationId', 'ASC')
86 2
            ->getQuery()
87 2
            ->getResult();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 3
    public function query(
94
        Specification $specification = null,
95
        array $orderings = null,
96
        int $maxResults = null,
97
        int $firstResult = null
98
    ): array {
99 3
        $queryBuilder =  $this->createQueryBuilder('n');
100 3
        if (!is_null($specification)) {
101 3
            $queryBuilder->where($specification->whereExpression('n'));
102
        }
103
        if (!is_null($orderings)) {
104
            foreach ($orderings as $sort => $order) {
105
                $queryBuilder->orderBy('n.' . $sort, $order);
106 3
            }
107
        }
108
        if (!is_null($maxResults)) {
109 3
            $queryBuilder->setMaxResults($maxResults);
110
        }
111
        if (!is_null($firstResult)) {
112
            $queryBuilder->setFirstResult($firstResult);
113 3
        }
114
115
        return $queryBuilder->getQuery()->getResult();
116
    }
117
}
118