NotificationRepository::addRecipientConditions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 15
cp 0
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\IdentifierRecipientInterface;
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 IdentifierRecipientInterface $recipient
29
     *
30
     * @return QueryBuilder
31
     */
32
    public function addRecipientConditions(QueryBuilder $builder, IdentifierRecipientInterface $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 IdentifierRecipientInterface  $recipient
51
     *
52
     * @return int
53
     */
54
    public function countUndeliveredRecipientNotification(IdentifierRecipientInterface $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 IdentifierRecipientInterface $recipient
68
     *
69
     * @return Notification[]
70
     */
71
    public function findUndeliveredRecipientNotification(IdentifierRecipientInterface $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 IdentifierRecipientInterface $recipient
84
     *
85
     * @return Notification[]
86
     */
87
    public function findAllForRecipient(IdentifierRecipientInterface $recipient)
88
    {
89
        $builder = $this->createQueryBuilder('notification');
90
91
        $this->addRecipientConditions($builder, $recipient);
92
93
        return $builder->getQuery()->getResult();
94
    }
95
}
96