Completed
Pull Request — master (#13)
by Matthieu
02:46
created

findUndeliveredRecipientNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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->getEntityManager()->createQueryBuilder();
57
        $builder
58
            ->from(Notification::class, 'notification')
59
            ->select('COUNT(notification)')
60
        ;
61
        $this->addRecipientConditions($builder, $recipient);
62
        $builder->andWhere($builder->expr()->isNull('notification.deliveredAt'));
63
64
        return intval($builder->getQuery()->getSingleScalarResult());
65
    }
66
67
    /**
68
     * @param DoctrineRecipientInterface $recipient
69
     *
70
     * @return array
71
     */
72
    public function findUndeliveredRecipientNotification(DoctrineRecipientInterface $recipient)
73
    {
74
        $builder = $this->createQueryBuilder('notification');
75
76
        $this->addRecipientConditions($builder, $recipient);
77
78
        $builder->andWhere($builder->expr()->isNull('notification.deliveredAt'));
79
80
        return $builder->getQuery()->getResult();
81
    }
82
83
    /**
84
     * @param DoctrineRecipientInterface $recipient
85
     *
86
     * @return Notification[]
87
     */
88
    public function findAllForRecipient(DoctrineRecipientInterface $recipient)
89
    {
90
        $builder = $this->createQueryBuilder('notification');
91
92
        $this->addRecipientConditions($builder, $recipient);
93
94
        return $builder->getQuery()->getResult();
95
    }
96
}
97