-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationManager.php
More file actions
173 lines (153 loc) · 4.93 KB
/
NotificationManager.php
File metadata and controls
173 lines (153 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace IrishDan\NotificationBundle;
use IrishDan\NotificationBundle\Broadcast\Broadcaster;
use IrishDan\NotificationBundle\Exception\BroadcastException;
use IrishDan\NotificationBundle\Notification\DatabaseNotificationInterface;
use IrishDan\NotificationBundle\Notification\NotifiableInterface;
use IrishDan\NotificationBundle\Notification\NotificationInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
/**
* Class NotificationManager
* This the primary public service.
* From here all notifications can be dispatched.
* Essentially is just a wrapper around the ContainerManager and DatabaseNotificationManager
*
* @package NotificationBundle
*/
class NotificationManager
{
/**
* @var ChannelManager
*/
protected $channelManager;
/**
* @var DatabaseNotificationManager
*/
protected $databaseNotificationManager;
/**
* @var
*/
protected $propertyAccessor;
/**
* @var array
*/
protected $broadcasters = [];
/**
* NotificationManager constructor.
*
* @param ChannelManager $channelManager
* @param DatabaseNotificationManager|null $databaseNotificationManager
*/
public function __construct(ChannelManager $channelManager, DatabaseNotificationManager $databaseNotificationManager = null)
{
$this->channelManager = $channelManager;
$this->databaseNotificationManager = $databaseNotificationManager;
}
/**
* @param $key
* @param Broadcaster $broadcaster
*/
public function setBroadcaster($key, Broadcaster $broadcaster)
{
$this->broadcasters[$key] = $broadcaster;
}
/**
* @param DatabaseNotificationManager $databaseNotificationManager
*/
public function setDatabaseNotificationManager(DatabaseNotificationManager $databaseNotificationManager)
{
$this->databaseNotificationManager = $databaseNotificationManager;
}
/**
* @param NotificationInterface $notification
* @param array | string $broadcasters
* @throws BroadcastException
*/
public function broadcast(NotificationInterface $notification, $broadcasters)
{
if (is_string($broadcasters)) {
$broadcasters = [$broadcasters];
}
foreach ($broadcasters as $broadcaster) {
if (empty($this->broadcasters[$broadcaster])) {
throw new BroadcastException(
sprintf('Broadcast channel with key "%s" does not exists', $broadcaster)
);
}
$this->broadcasters[$broadcaster]->broadcast($notification);
}
}
/**
* @param NotificationInterface $notification
* @param $recipients
* @param array $data
*/
public function send(NotificationInterface $notification, $recipients, array $data = [])
{
if (!is_array($recipients)) {
$recipients = [$recipients];
}
if (!empty($data)) {
if (empty($this->propertyAccessor)) {
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}
$dataArray = $notification->getDataArray();
foreach ($data as $key => $value) {
$this->propertyAccessor->setValue($dataArray, '[' . $key . ']', $value);
}
$notification->setDataArray($dataArray);
}
$this->channelManager->send($recipients, $notification);
}
/**
* @param DatabaseNotificationInterface $notification
* @return bool
*/
public function markAsRead(DatabaseNotificationInterface $notification)
{
$now = new \DateTime();
$this->databaseNotificationManager->setReadAtDate($notification, $now);
}
/**
* @param NotifiableInterface $user
* @return bool
*/
public function markAllAsRead(NotifiableInterface $user)
{
$now = new \DateTime();
$this->databaseNotificationManager->setUsersNotificationsAsRead($user, $now);
}
/**
* @param NotifiableInterface $user
* @return int
*/
public function allNotificationCount(NotifiableInterface $user)
{
return $this->notificationCount($user);
}
/**
* @param NotifiableInterface $user
* @return int
*/
public function unreadNotificationCount(NotifiableInterface $user)
{
return $this->notificationCount($user, 'unread');
}
/**
* @param NotifiableInterface $user
* @return int
*/
public function readNotificationCount(NotifiableInterface $user)
{
return $this->notificationCount($user, 'read');
}
/**
* @param NotifiableInterface $user
* @param string $status
* @return int
*/
public function notificationCount(NotifiableInterface $user, $status = '')
{
return $this->databaseNotificationManager->getUsersNotificationCount($user, $status);
}
}