NotificationRepository::getUnreadNotifications()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use IrishDan\NotificationBundle\DatabaseNotifiableInterface;
7
8
class NotificationRepository extends EntityRepository implements DatabaseNotificationRepositoryInterface
9
{
10
    public function getNotificationsCount(DatabaseNotifiableInterface $user, $status = '')
11
    {
12
        $dq = $this->createQueryBuilder('n')
13
            ->select('count(n.id)')
14
            ->andWhere('n.notifiable = ' . $user->getId());
15
16
        switch ($status) {
17
            case 'read':
18
                $dq->andWhere('n.readAt IS NOT NULL');
19
                break;
20
            case 'unread':
21
                $dq->andWhere('n.readAt IS NULL');
22
                break;
23
        }
24
25
        $count = $dq->getQuery()->getSingleScalarResult();
26
27
        return $count;
28
    }
29
30
    public function getUnreadNotifications(DatabaseNotifiableInterface $user)
31
    {
32
        return $this->findBy([
33
            'notifiable' => $user,
34
            'readAt' => null,
35
        ]);
36
    }
37
}
38