Completed
Push — master ( 2f5ecd...f26b52 )
by dan
01:56
created

DatabaseNotificationManager.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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)
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