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