|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Azine\EmailBundle\Entity\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* NotificationRepository |
|
9
|
|
|
* |
|
10
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
|
11
|
|
|
* repository methods below. |
|
12
|
|
|
*/ |
|
13
|
|
|
class NotificationRepository extends EntityRepository{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get all notifications that should be sent. |
|
17
|
|
|
* @param $recipientId |
|
18
|
|
|
* @return array of Notification |
|
19
|
|
|
*/ |
|
20
|
|
View Code Duplication |
public function getNotificationsToSend($recipientId){ |
|
|
|
|
|
|
21
|
|
|
$qb = $this->createQueryBuilder() |
|
|
|
|
|
|
22
|
|
|
->select("n") |
|
23
|
|
|
->from("Azine\EmailBundle\Entity\Notification", "n") |
|
24
|
|
|
->andWhere("n.sent is null") |
|
25
|
|
|
->andWhere("n.recipient_id = :recipientId") |
|
26
|
|
|
->setParameter('recipientId', $recipientId) |
|
27
|
|
|
->orderBy("n.importance", "desc") |
|
28
|
|
|
->orderBy("n.template", "asc") |
|
29
|
|
|
->orderBy("n.title", "asc"); |
|
30
|
|
|
$notifications = $qb->getQuery()->execute(); |
|
31
|
|
|
return $notifications; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get all notifications that should be sent immediately. |
|
36
|
|
|
* @param $recipientId |
|
37
|
|
|
* @return array of Notification |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
public function getNotificationsToSendImmediately($recipientId){ |
|
|
|
|
|
|
40
|
|
|
$qb = $this->createQueryBuilder() |
|
|
|
|
|
|
41
|
|
|
->select("n") |
|
42
|
|
|
->from("Azine\EmailBundle\Entity\Notification", "n") |
|
43
|
|
|
->andWhere("n.sent is null") |
|
44
|
|
|
->andWhere("n.send_immediately = true") |
|
45
|
|
|
->andWhere("n.recipient_id = :recipientId") |
|
46
|
|
|
->setParameter('recipientId', $recipientId) |
|
47
|
|
|
->orderBy("n.importance", "desc") |
|
48
|
|
|
->orderBy("n.template", "asc") |
|
49
|
|
|
->orderBy("n.title", "asc"); |
|
50
|
|
|
$notifications = $qb->getQuery()->execute(); |
|
51
|
|
|
return $notifications; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get all recipients with unsent Notifications |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getNotificationRecipientIds(){ |
|
59
|
|
|
$qb = $this->createQueryBuilder() |
|
|
|
|
|
|
60
|
|
|
->select("n.recipient_id") |
|
61
|
|
|
->distinct() |
|
62
|
|
|
->from("Azine\EmailBundle\Entity\Notification", "n") |
|
63
|
|
|
->andWhere("n.sent is null"); |
|
64
|
|
|
$results = $qb->getQuery()->execute(); |
|
65
|
|
|
|
|
66
|
|
|
$ids = array(); |
|
67
|
|
|
foreach ($results as $next) { |
|
68
|
|
|
$ids[] = $next['recipient_id']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $ids; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Mark all notifications as sent "far in the past". This is used for users that don't want to receive any notifications. |
|
76
|
|
|
* @param $recipientId |
|
77
|
|
|
*/ |
|
78
|
|
|
public function markAllNotificationsAsSentFarInThePast($recipientId){ |
|
79
|
|
|
$qb = $this->createQueryBuilder() |
|
|
|
|
|
|
80
|
|
|
->update("Azine\EmailBundle\Entity\Notification", "n") |
|
81
|
|
|
->set("n.sent", ":farInThePast") |
|
82
|
|
|
->andWhere("n.sent is null") |
|
83
|
|
|
->andWhere("n.recipient_id = :recipientId") |
|
84
|
|
|
->setParameter('recipientId', $recipientId) |
|
85
|
|
|
->setParameter('farInThePast', new \DateTime('1900-01-01')); |
|
86
|
|
|
$qb->getQuery()->execute(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the \DateTime of the last Notification that has been sent. |
|
91
|
|
|
* @param $recipientId |
|
92
|
|
|
* @return \DateTime |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getLastNotificationDate($recipientId){ |
|
95
|
|
|
$qb = $this->createQueryBuilder() |
|
|
|
|
|
|
96
|
|
|
->select("max(n.sent)") |
|
97
|
|
|
->from("Azine\EmailBundle\Entity\Notification", "n") |
|
98
|
|
|
->andWhere("n.recipient_id = :recipientId") |
|
99
|
|
|
->setParameter('recipientId', $recipientId); |
|
100
|
|
|
$results = $qb->getQuery()->execute(); |
|
101
|
|
|
if ($results[0][1] == null) { |
|
102
|
|
|
// the user has not received any notifications yet ever |
|
103
|
|
|
$lastNotification = new \DateTime("@0"); |
|
104
|
|
|
} else { |
|
105
|
|
|
$lastNotification = new \DateTime($results[0][1]); |
|
106
|
|
|
} |
|
107
|
|
|
return $lastNotification; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.