Completed
Push — master ( df9f00...ccd20f )
by dan
01:37
created

setUsersNotificationsAsRead()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 2
1
<?php
2
3
namespace IrishDan\NotificationBundle;
4
5
use Doctrine\ORM\EntityManager;
6
use IrishDan\NotificationBundle\Notification\DatabaseNotificationInterface;
7
use IrishDan\NotificationBundle\Notification\NotifiableInterface;
8
9
class DatabaseNotificationManager
10
{
11
    private $databaseConfiguration;
12
    private $entityManager;
13
14
    public function __construct(EntityManager $entityManager, array $databaseConfiguration = [])
15
    {
16
        $this->entityManager = $entityManager;
17
        $this->databaseConfiguration = $databaseConfiguration;
18
    }
19
20
    public function setReadAtDate(DatabaseNotificationInterface $notification, $now = null, $flush = true)
21
    {
22
        if (empty($now)) {
23
            $now = new \DateTime();
24
        }
25
26
        $notification->setReadAt($now);
27
28
        $this->entityManager->persist($notification);
29
        if ($flush) {
30
            $this->entityManager->flush();
31
        }
32
    }
33
34
    public function setUsersNotificationsAsRead(NotifiableInterface $notifiable, $now = null)
0 ignored issues
show
Unused Code introduced by
The parameter $now is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $entity = $this->notificationEntityName();
37
        if (!empty($entity)) {
38
            $options = [
39
                'notifiable' => $notifiable,
40
                'readAt' => null,
41
            ];
42
            $usersNotifications = $this->entityManager->getRepository($entity)->findBy($options);
43
44
            if (!empty($usersNotifications)) {
45
                foreach ($usersNotifications as $notification) {
46
                    $this->setReadAtDate($notification, null, false);
47
                }
48
            }
49
            $this->entityManager->flush();
50
        }
51
    }
52
53
    public function getUsersNotificationCount(NotifiableInterface $user, $status = '')
54
    {
55
        $entity = $this->notificationEntityName();
56
        if (!empty($entity)) {
57
            $count = $this->entityManager->getRepository($entity)->getNotificationsCount($user, $status);
58
59
            return $count;
60
        }
61
62
        return 0;
63
    }
64
65
    protected function notificationEntityName()
66
    {
67
        $config = $this->databaseConfiguration;
68
        if (!empty($config['entity'])) {
69
            return $config['entity'];
70
        }
71
72
        return false;
73
    }
74
}
75