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