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
|
|
|
|