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 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
|
|
|
/** |
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 LogicException( |
41
|
1 |
|
sprintf( |
42
|
1 |
|
'Notification of deduplication key (%s) already exists.', |
43
|
1 |
|
$deduplicationKey |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
3 |
|
$this->persist($notification); |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
* @throws ORMException |
53
|
|
|
* @throws OptimisticLockException |
54
|
|
|
*/ |
55
|
3 |
|
public function persist(Notification $notification): void |
56
|
|
|
{ |
57
|
3 |
|
$this->getEntityManager()->persist($notification); |
58
|
3 |
|
if ($this->isPrecocious) { |
59
|
3 |
|
$this->getEntityManager()->flush(); |
60
|
|
|
} |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function notificationOfId(NotificationId $notificationId): ?Notification |
67
|
|
|
{ |
68
|
|
|
return $this->find($notificationId); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
2 |
|
public function hasNotificationOfDeduplicationKey(DeduplicationKey $deduplicationKey): bool |
75
|
|
|
{ |
76
|
2 |
|
return !is_null($this->findOneBy(['deduplicationKey' => $deduplicationKey])); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
2 |
|
public function freshNotifications(): array |
83
|
|
|
{ |
84
|
2 |
|
return $this->createQueryBuilder('n') |
85
|
2 |
|
->where('n.sentAt IS NULL') |
86
|
2 |
|
->andWhere('n.failedAt IS NULL') |
87
|
2 |
|
->orderBy('n.notificationId', 'ASC') |
88
|
2 |
|
->getQuery() |
89
|
2 |
|
->getResult(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|