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\NotificationId; |
14
|
|
|
use Shippinno\Notification\Domain\Model\NotificationRepository; |
15
|
|
|
|
16
|
|
|
class DoctrineNotificationRepository extends EntityRepository implements NotificationRepository |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $isPrecocious; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
* @param bool $isPrecocious |
26
|
|
|
*/ |
27
|
3 |
|
public function __construct($em, ClassMetadata $class, bool $isPrecocious) |
28
|
|
|
{ |
29
|
3 |
|
$this->isPrecocious = $isPrecocious; |
30
|
3 |
|
parent::__construct($em, $class); |
31
|
3 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
3 |
|
public function add(Notification $notification, bool $precocious = false): void |
37
|
|
|
{ |
38
|
3 |
|
$deduplicationKey = $notification->deduplicationKey(); |
39
|
3 |
|
if (!is_null($deduplicationKey) && $this->hasNotificationOfDeduplicationKey($deduplicationKey)) { |
40
|
1 |
|
throw new DeduplicationException($deduplicationKey); |
41
|
|
|
} |
42
|
3 |
|
$this->persist($notification); |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
* @throws ORMException |
48
|
|
|
* @throws OptimisticLockException |
49
|
|
|
*/ |
50
|
3 |
|
public function persist(Notification $notification): void |
51
|
|
|
{ |
52
|
3 |
|
$this->getEntityManager()->persist($notification); |
53
|
3 |
|
if ($this->isPrecocious) { |
54
|
3 |
|
$this->getEntityManager()->flush(); |
55
|
|
|
} |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function notificationOfId(NotificationId $notificationId): ?Notification |
62
|
|
|
{ |
63
|
|
|
return $this->find($notificationId); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
2 |
|
public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool |
70
|
|
|
{ |
71
|
2 |
|
return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey])); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
2 |
|
public function freshNotifications(): array |
78
|
|
|
{ |
79
|
2 |
|
return $this->createQueryBuilder('n') |
80
|
2 |
|
->where('n.sentAt IS NULL') |
81
|
2 |
|
->andWhere('n.failedAt IS NULL') |
82
|
2 |
|
->andWhere('n.lockedAt IS NULL') |
83
|
2 |
|
->orderBy('n.notificationId', 'ASC') |
84
|
2 |
|
->getQuery() |
85
|
2 |
|
->getResult(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|