NotificationRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 49
c 2
b 0
f 0
dl 0
loc 107
ccs 0
cts 53
cp 0
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getNotificationRecipientIds() 0 15 2
A getNotificationsToSendImmediately() 0 13 1
A getNotificationsToSend() 0 12 1
A getLastNotificationDate() 0 16 2
A markAllNotificationsAsSentFarInThePast() 0 10 1
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
     *
18
     * @param $recipientId
19
     *
20
     * @return array of Notification
21
     */
22
    public function getNotificationsToSend($recipientId)
23
    {
24
        $qb = $this->createQueryBuilder('n')
25
            ->andWhere('n.sent is null')
26
            ->andWhere('n.recipient_id = :recipientId')
27
            ->setParameter('recipientId', $recipientId)
28
            ->orderBy('n.importance', 'desc')
29
            ->orderBy('n.template', 'asc')
30
            ->orderBy('n.title', 'asc');
31
        $notifications = $qb->getQuery()->execute();
32
33
        return $notifications;
34
    }
35
36
    /**
37
     * Get all notifications that should be sent immediately.
38
     *
39
     * @param $recipientId
40
     *
41
     * @return array of Notification
42
     */
43
    public function getNotificationsToSendImmediately($recipientId)
44
    {
45
        $qb = $this->createQueryBuilder('n')
46
            ->andWhere('n.sent is null')
47
            ->andWhere('n.send_immediately = true')
48
            ->andWhere('n.recipient_id = :recipientId')
49
            ->setParameter('recipientId', $recipientId)
50
            ->orderBy('n.importance', 'desc')
51
            ->orderBy('n.template', 'asc')
52
            ->orderBy('n.title', 'asc');
53
        $notifications = $qb->getQuery()->execute();
54
55
        return $notifications;
56
    }
57
58
    /**
59
     * Get all recipients with unsent Notifications.
60
     *
61
     * @return array
62
     */
63
    public function getNotificationRecipientIds()
64
    {
65
        $qb = $this->getEntityManager()->createQueryBuilder()
66
            ->select('n.recipient_id')
67
            ->distinct()
68
            ->from("Azine\EmailBundle\Entity\Notification", 'n')
69
            ->andWhere('n.sent is null');
70
        $results = $qb->getQuery()->execute();
71
72
        $ids = array();
73
        foreach ($results as $next) {
74
            $ids[] = $next['recipient_id'];
75
        }
76
77
        return $ids;
78
    }
79
80
    /**
81
     * Mark all notifications as sent "far in the past". This is used for users that don't want to receive any notifications.
82
     *
83
     * @param $recipientId
84
     */
85
    public function markAllNotificationsAsSentFarInThePast($recipientId)
86
    {
87
        $qb = $this->getEntityManager()->createQueryBuilder()
88
            ->update("Azine\EmailBundle\Entity\Notification", 'n')
89
            ->set('n.sent', ':farInThePast')
90
            ->andWhere('n.sent is null')
91
            ->andWhere('n.recipient_id = :recipientId')
92
            ->setParameter('recipientId', $recipientId)
93
            ->setParameter('farInThePast', new \DateTime('1900-01-01'));
94
        $qb->getQuery()->execute();
95
    }
96
97
    /**
98
     * Get the \DateTime of the last Notification that has been sent.
99
     *
100
     * @param $recipientId
101
     *
102
     * @return \DateTime
103
     */
104
    public function getLastNotificationDate($recipientId)
105
    {
106
        $qb = $this->getEntityManager()->createQueryBuilder()
107
            ->select('max(n.sent)')
108
            ->from("Azine\EmailBundle\Entity\Notification", 'n')
109
            ->andWhere('n.recipient_id = :recipientId')
110
            ->setParameter('recipientId', $recipientId);
111
        $results = $qb->getQuery()->execute();
112
        if (null === $results[0][1]) {
113
            // the user has not received any notifications yet ever
114
            $lastNotification = new \DateTime('@0');
115
        } else {
116
            $lastNotification = new \DateTime($results[0][1]);
117
        }
118
119
        return $lastNotification;
120
    }
121
}
122