1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Broadcast\Broadcaster; |
6
|
|
|
use IrishDan\NotificationBundle\Notification\DatabaseNotificationInterface; |
7
|
|
|
use IrishDan\NotificationBundle\Notification\NotifiableInterface; |
8
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class NotificationManager |
13
|
|
|
* This the primary public service. |
14
|
|
|
* From here all notifications can be dispatched. |
15
|
|
|
* Essentially is just a wrapper around the ContainerManager and DatabaseNotificationManager |
16
|
|
|
* |
17
|
|
|
* @package NotificationBundle |
18
|
|
|
*/ |
19
|
|
|
class NotificationManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ChannelManager |
23
|
|
|
*/ |
24
|
|
|
protected $channelManager; |
25
|
|
|
/** |
26
|
|
|
* @var DatabaseNotificationManager |
27
|
|
|
*/ |
28
|
|
|
protected $databaseNotificationManager; |
29
|
|
|
protected $propertyAccessor; |
30
|
|
|
protected $broadcasters = []; |
31
|
|
|
|
32
|
|
|
public function __construct(ChannelManager $channelManager, DatabaseNotificationManager $databaseNotificationManager = null) |
33
|
|
|
{ |
34
|
|
|
$this->channelManager = $channelManager; |
35
|
|
|
$this->databaseNotificationManager = $databaseNotificationManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setBroadcaster($key, Broadcaster $broadcaster) |
39
|
|
|
{ |
40
|
|
|
$this->broadcasters[$key] = $broadcaster; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setDatabaseNotificationManager(DatabaseNotificationManager $databaseNotificationManager) |
44
|
|
|
{ |
45
|
|
|
$this->databaseNotificationManager = $databaseNotificationManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function broadcast(NotificationInterface $notification, array $broadcasters) |
49
|
|
|
{ |
50
|
|
|
foreach ($broadcasters as $broadcaster) { |
51
|
|
|
if (empty($this->broadcasters[$broadcaster])) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$this->broadcasters[$broadcaster]->broadcast($notification); |
57
|
|
|
} catch (\Exception $exception) { |
58
|
|
|
// @TODO: |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function send(NotificationInterface $notification, $recipients, array $data = []) |
64
|
|
|
{ |
65
|
|
|
if (!is_array($recipients)) { |
66
|
|
|
$recipients = [$recipients]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!empty($data)) { |
70
|
|
|
if (empty($this->propertyAccessor)) { |
71
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$dataArray = $notification->getDataArray(); |
75
|
|
|
foreach ($data as $key => $value) { |
76
|
|
|
$this->propertyAccessor->setValue($dataArray, '[' . $key . ']', $value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$notification->setDataArray($dataArray); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->channelManager->send($recipients, $notification); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function markAsRead(DatabaseNotificationInterface $notification) |
86
|
|
|
{ |
87
|
|
|
$now = new \DateTime(); |
88
|
|
|
try { |
89
|
|
|
$this->databaseNotificationManager->setReadAtDate($notification, $now); |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} catch (\Exception $exception) { |
93
|
|
|
// @TODO: |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function markAllAsRead(NotifiableInterface $user) |
98
|
|
|
{ |
99
|
|
|
$now = new \DateTime(); |
100
|
|
|
try { |
101
|
|
|
$this->databaseNotificationManager->setUsersNotificationsAsRead($user, $now); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} catch (\Exception $exception) { |
105
|
|
|
// @TODO: |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function allNotificationCount(NotifiableInterface $user) |
110
|
|
|
{ |
111
|
|
|
return $this->notificationCount($user); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function unreadNotificationCount(NotifiableInterface $user) |
115
|
|
|
{ |
116
|
|
|
return $this->notificationCount($user, 'unread'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function readNotificationCount(NotifiableInterface $user) |
120
|
|
|
{ |
121
|
|
|
return $this->notificationCount($user, 'read'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function notificationCount(NotifiableInterface $user, $status = '') |
125
|
|
|
{ |
126
|
|
|
return $this->databaseNotificationManager->getUsersNotificationCount($user, $status); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|