Completed
Push — master ( 80f1a6...8c34ac )
by Dominik
03:50
created

markAllNotificationsAsSentFarInThePast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 2
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){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
21
        $qb = $this->createQueryBuilder()
0 ignored issues
show
Bug introduced by
The call to createQueryBuilder() misses a required argument $alias.

This check looks for function calls that miss required arguments.

Loading history...
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){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
40
        $qb = $this->createQueryBuilder()
0 ignored issues
show
Bug introduced by
The call to createQueryBuilder() misses a required argument $alias.

This check looks for function calls that miss required arguments.

Loading history...
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()
0 ignored issues
show
Bug introduced by
The call to createQueryBuilder() misses a required argument $alias.

This check looks for function calls that miss required arguments.

Loading history...
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()
0 ignored issues
show
Bug introduced by
The call to createQueryBuilder() misses a required argument $alias.

This check looks for function calls that miss required arguments.

Loading history...
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()
0 ignored issues
show
Bug introduced by
The call to createQueryBuilder() misses a required argument $alias.

This check looks for function calls that miss required arguments.

Loading history...
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