|
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; |
|
8
|
|
|
use Doctrine\ORM\OptimisticLockException; |
|
9
|
|
|
use Doctrine\ORM\ORMException; |
|
10
|
|
|
use LogicException; |
|
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
|
3 |
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
3 |
|
*/ |
|
21
|
3 |
|
private $isPrecocious; |
|
22
|
1 |
|
|
|
23
|
1 |
|
/** |
|
24
|
1 |
|
* @param $em |
|
25
|
1 |
|
* @param Mapping\ClassMetadata $class |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($em, Mapping\ClassMetadata $class, bool $isPrecocious) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->isPrecocious = $isPrecocious; |
|
30
|
3 |
|
parent::__construct($em, $class); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function add(Notification $notification, bool $precocious = false): void |
|
37
|
|
|
{ |
|
38
|
|
|
$deduplicationKey = $notification->deduplicationKey(); |
|
39
|
|
|
if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) { |
|
40
|
|
|
throw new LogicException( |
|
41
|
|
|
sprintf( |
|
42
|
|
|
'Notification of deduplication key (%s) already exists.', |
|
43
|
2 |
|
$deduplicationKey |
|
44
|
|
|
) |
|
45
|
2 |
|
); |
|
46
|
|
|
} |
|
47
|
|
|
$this->persist($notification); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
2 |
|
* {@inheritdoc} |
|
52
|
|
|
* @throws ORMException |
|
53
|
2 |
|
* @throws OptimisticLockException |
|
54
|
2 |
|
*/ |
|
55
|
2 |
|
public function markSent(Notification $notification): void |
|
56
|
2 |
|
{ |
|
57
|
2 |
|
$notification->markSent(); |
|
58
|
|
|
$this->persist($notification); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
* @throws ORMException |
|
64
|
|
|
* @throws OptimisticLockException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function markFailed(Notification $notification, string $reason): void |
|
67
|
|
|
{ |
|
68
|
|
|
$notification->markFailed($reason); |
|
69
|
|
|
$this->persist($notification); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Notification $notification |
|
74
|
|
|
* @throws ORMException |
|
75
|
|
|
* @throws OptimisticLockException |
|
76
|
|
|
*/ |
|
77
|
|
|
private function persist(Notification $notification): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->getEntityManager()->persist($notification); |
|
80
|
|
|
if ($this->isPrecocious) { |
|
81
|
|
|
$this->getEntityManager()->flush(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function notificationOfId(NotificationId $notificationId): ?Notification |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->find($notificationId); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey])); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function unsentNotifications(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->createQueryBuilder('n') |
|
107
|
|
|
->where('n.sentAt IS NULL') |
|
108
|
|
|
->orderBy('n.notificationId', 'ASC') |
|
109
|
|
|
->getQuery() |
|
110
|
|
|
->getResult(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|