Passed
Pull Request — master (#2)
by Hikaru
08:17
created

DoctrineNotificationRepository::removeAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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 5
    public function __construct($em, ClassMetadata $class, bool $isPrecocious)
30
    {
31 5
        $this->isPrecocious = $isPrecocious;
32 5
        parent::__construct($em, $class);
33 5
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 5
    public function add(Notification $notification, bool $precocious = false): void
39
    {
40 5
        $deduplicationKey = $notification->deduplicationKey();
41 5
        if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) {
42 1
            throw new DeduplicationException($deduplicationKey);
43
        }
44 5
        $this->persist($notification);
45 5
    }
46
47
    /**
48
     * {@inheritdoc}
49
     * @throws ORMException
50
     * @throws OptimisticLockException
51
     */
52 5
    public function persist(Notification $notification): void
53
    {
54 5
        $this->getEntityManager()->persist($notification);
55 5
        if ($this->isPrecocious) {
56 5
            $this->getEntityManager()->flush();
57
        }
58 5
    }
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 3
    public function freshNotifications(): array
80
    {
81 3
        return $this->createQueryBuilder('n')
82 3
            ->where('n.sentAt IS NULL')
83 3
            ->andWhere('n.failedAt IS NULL')
84 3
            ->andWhere('n.lockedAt IS NULL')
85 3
            ->orderBy('n.notificationId', 'ASC')
86 3
            ->getQuery()
87 3
            ->getResult();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 4
    public function query(
94
        Specification $specification = null,
95
        array $orderings = null,
96
        int $maxResults = null,
97
        int $firstResult = null
98
    ): array {
99 4
        $queryBuilder =  $this->createQueryBuilder('n');
100 4
        if (!is_null($specification)) {
101 4
            $queryBuilder->where($specification->whereExpression('n'));
102
        }
103 4
        if (!is_null($orderings)) {
104
            foreach ($orderings as $sort => $order) {
105
                $queryBuilder->orderBy('n.' . $sort, $order);
106
            }
107
        }
108 4
        if (!is_null($maxResults)) {
109
            $queryBuilder->setMaxResults($maxResults);
110
        }
111 4
        if (!is_null($firstResult)) {
112
            $queryBuilder->setFirstResult($firstResult);
113
        }
114
115 4
        return $queryBuilder->getQuery()->getResult();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     * @throws ORMException
121
     */
122 1
    public function remove(Notification $notification): void
123
    {
124 1
        $this->getEntityManager()->remove($notification);
125 1
    }
126
127
    /**
128
     * {@inheritdoc}
129
     * @throws ORMException
130
     */
131 1
    public function removeAll(array $notifications): void
132
    {
133 1
        foreach ($notifications as $notification) {
134 1
            $this->remove($notification);
135
        }
136 1
    }
137
}
138