|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\MessengerBundle\Entity\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
8
|
|
|
use Yokai\MessengerBundle\Entity\Notification; |
|
9
|
|
|
use Yokai\MessengerBundle\Recipient\DoctrineRecipientInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Yann Eugoné <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class NotificationRepository extends EntityRepository |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param Notification $notification |
|
18
|
|
|
*/ |
|
19
|
|
|
public function setNotificationAsDelivered(Notification $notification) |
|
20
|
|
|
{ |
|
21
|
|
|
$notification->setDelivered(); |
|
22
|
|
|
$this->getEntityManager()->persist($notification); |
|
23
|
|
|
$this->getEntityManager()->flush($notification); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param QueryBuilder $builder |
|
28
|
|
|
* @param DoctrineRecipientInterface $recipient |
|
29
|
|
|
* |
|
30
|
|
|
* @return QueryBuilder |
|
31
|
|
|
*/ |
|
32
|
|
|
public function addRecipientConditions(QueryBuilder $builder, DoctrineRecipientInterface $recipient) |
|
33
|
|
|
{ |
|
34
|
|
|
$alias = $builder->getRootAliases()[0]; |
|
35
|
|
|
$builder |
|
36
|
|
|
->where( |
|
37
|
|
|
$builder->expr()->andX( |
|
38
|
|
|
$builder->expr()->eq($alias . '.recipientClass', ':class'), |
|
39
|
|
|
$builder->expr()->eq($alias . '.recipientId', ':id') |
|
40
|
|
|
) |
|
41
|
|
|
) |
|
42
|
|
|
->setParameter('class', ClassUtils::getClass($recipient)) |
|
43
|
|
|
->setParameter('id', $recipient->getId()) |
|
44
|
|
|
; |
|
45
|
|
|
|
|
46
|
|
|
return $builder; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param DoctrineRecipientInterface $recipient |
|
51
|
|
|
* |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
public function countUndeliveredRecipientNotification(DoctrineRecipientInterface $recipient) |
|
55
|
|
|
{ |
|
56
|
|
|
$builder = $this->createQueryBuilder('notification'); |
|
57
|
|
|
$builder |
|
58
|
|
|
->select('COUNT(notification)') |
|
59
|
|
|
; |
|
60
|
|
|
$this->addRecipientConditions($builder, $recipient); |
|
61
|
|
|
$builder->andWhere($builder->expr()->isNull('notification.deliveredAt')); |
|
62
|
|
|
|
|
63
|
|
|
return intval($builder->getQuery()->getSingleScalarResult()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param DoctrineRecipientInterface $recipient |
|
68
|
|
|
* |
|
69
|
|
|
* @return Notification[] |
|
70
|
|
|
*/ |
|
71
|
|
|
public function findUndeliveredRecipientNotification(DoctrineRecipientInterface $recipient) |
|
72
|
|
|
{ |
|
73
|
|
|
$builder = $this->createQueryBuilder('notification'); |
|
74
|
|
|
|
|
75
|
|
|
$this->addRecipientConditions($builder, $recipient); |
|
76
|
|
|
|
|
77
|
|
|
$builder->andWhere($builder->expr()->isNull('notification.deliveredAt')); |
|
78
|
|
|
|
|
79
|
|
|
return $builder->getQuery()->getResult(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param DoctrineRecipientInterface $recipient |
|
84
|
|
|
* |
|
85
|
|
|
* @return Notification[] |
|
86
|
|
|
*/ |
|
87
|
|
|
public function findAllForRecipient(DoctrineRecipientInterface $recipient) |
|
88
|
|
|
{ |
|
89
|
|
|
$builder = $this->createQueryBuilder('notification'); |
|
90
|
|
|
|
|
91
|
|
|
$this->addRecipientConditions($builder, $recipient); |
|
92
|
|
|
|
|
93
|
|
|
return $builder->getQuery()->getResult(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|