|
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) |
|
|
|
|
|
|
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
|
|
|
|
The
EntityManagermight 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:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.