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