Completed
Push — master ( 40fe4a...13aa34 )
by dan
01:54
created

notificationEntityName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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 = [])
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

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