1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Notification\DatabaseNotificationInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Notification\NotifiableInterface; |
7
|
|
|
use Symfony\Bridge\Doctrine\ManagerRegistry; |
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class DatabaseNotificationManager |
13
|
|
|
* |
14
|
|
|
* @package IrishDan\NotificationBundle |
15
|
|
|
*/ |
16
|
|
|
class DatabaseNotificationManager |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $databaseConfiguration; |
22
|
|
|
/** |
23
|
|
|
* @var ManagerRegistry |
24
|
|
|
*/ |
25
|
|
|
protected $managerRegistry; |
26
|
|
|
/** |
27
|
|
|
* @var PropertyAccessor |
28
|
|
|
*/ |
29
|
|
|
protected $propertyAccessor; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DatabaseNotificationManager constructor. |
33
|
|
|
* |
34
|
|
|
* @param ManagerRegistry $managerRegistry |
35
|
|
|
* @param array $databaseConfiguration |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ManagerRegistry $managerRegistry, array $databaseConfiguration = []) |
38
|
|
|
{ |
39
|
|
|
$this->managerRegistry = $managerRegistry; |
40
|
|
|
$this->databaseConfiguration = $databaseConfiguration; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getEntityManager() |
44
|
|
|
{ |
45
|
|
|
$entity = $this->notificationEntityName(); |
46
|
|
|
if ($entity) { |
47
|
|
|
return $this->managerRegistry->getManagerForClass($entity); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $data |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function createDatabaseNotification(array $data) |
58
|
|
|
{ |
59
|
|
|
if ($this->propertyAccessor === null) { |
60
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$entityManager = $this->getEntityManager(); |
64
|
|
|
if ($entityManager) { |
65
|
|
|
$entity = $this->notificationEntityName(); |
66
|
|
|
$class = $entityManager->getRepository($entity)->getClassName(); |
67
|
|
|
$databaseNotification = new $class(); |
68
|
|
|
|
69
|
|
|
// Transfer values from message to databaseNotification. |
70
|
|
|
$properties = ['notifiable', 'uuid', 'type', 'body', 'title']; |
71
|
|
|
foreach ($properties as $property) { |
72
|
|
|
$value = $this->propertyAccessor->getValue($data, '[' . $property . ']'); |
73
|
|
|
$this->propertyAccessor->setValue($databaseNotification, $property, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Save the notification to the database |
77
|
|
|
$entityManager->persist($databaseNotification); |
78
|
|
|
$entityManager->flush(); |
79
|
|
|
|
80
|
|
|
return $databaseNotification; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param DatabaseNotificationInterface $notification |
88
|
|
|
* @param null $now |
89
|
|
|
* @param bool $flush |
90
|
|
|
*/ |
91
|
|
|
public function setReadAtDate(DatabaseNotificationInterface $notification, $now = null, $flush = true) |
92
|
|
|
{ |
93
|
|
|
if (empty($now)) { |
94
|
|
|
$now = new \DateTime(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$notification->setReadAt($now); |
98
|
|
|
|
99
|
|
|
$entityManager = $this->getEntityManager(); |
100
|
|
|
$entityManager->persist($notification); |
101
|
|
|
if ($flush) { |
102
|
|
|
$entityManager->flush(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param NotifiableInterface $notifiable |
108
|
|
|
* @param null $now |
109
|
|
|
*/ |
110
|
|
|
public function setUsersNotificationsAsRead(NotifiableInterface $notifiable, $now = null) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$entity = $this->notificationEntityName(); |
113
|
|
|
if (!empty($entity)) { |
114
|
|
|
$options = [ |
115
|
|
|
'notifiable' => $notifiable, |
116
|
|
|
'readAt' => null, |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
$entityManager = $this->getEntityManager(); |
120
|
|
|
$usersNotifications = $entityManager->getRepository($entity)->findBy($options); |
121
|
|
|
|
122
|
|
|
if (!empty($usersNotifications)) { |
123
|
|
|
foreach ($usersNotifications as $notification) { |
124
|
|
|
$this->setReadAtDate($notification, null, false); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$entityManager->flush(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getUsersUnreadNotifications(DatabaseNotifiableInterface $user) |
132
|
|
|
{ |
133
|
|
|
$entityManager = $this->getEntityManager(); |
134
|
|
|
if ($entityManager) { |
135
|
|
|
$entity = $this->notificationEntityName(); |
136
|
|
|
$notifications = $entityManager->getRepository($entity)->getUnreadNotifications($user); |
137
|
|
|
|
138
|
|
|
return $notifications; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return []; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param NotifiableInterface $user |
146
|
|
|
* @param string $status |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
|
|
public function getUsersNotificationCount(NotifiableInterface $user, $status = '') |
150
|
|
|
{ |
151
|
|
|
$entityManager = $this->getEntityManager(); |
152
|
|
|
if ($entityManager) { |
153
|
|
|
$entity = $this->notificationEntityName(); |
154
|
|
|
$count = $entityManager->getRepository($entity)->getNotificationsCount($user, $status); |
155
|
|
|
|
156
|
|
|
return $count; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return 0; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return bool|mixed |
164
|
|
|
*/ |
165
|
|
|
protected function notificationEntityName() |
166
|
|
|
{ |
167
|
|
|
$config = $this->databaseConfiguration; |
168
|
|
|
if (!empty($config['entity'])) { |
169
|
|
|
return $config['entity']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.