-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifier.php
More file actions
137 lines (115 loc) · 3.22 KB
/
Notifier.php
File metadata and controls
137 lines (115 loc) · 3.22 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
<?php
namespace ITE\Js\Notification;
use ITE\Js\Notification\Channel\ChannelInterface;
use ITE\Js\Notification\Channel\NotificationAliasTrait;
use ITE\Js\Notification\Collector\CollectorInterface;
/**
* Class Notifier
*
* @author sam0delkin <t.samodelkin@gmail.com>
*/
class Notifier implements NotifierInterface
{
use NotificationAliasTrait;
/**
* @var CollectorInterface[]
*/
protected $collectors = [];
/**
* @var ChannelInterface[]
*/
protected $channels = [];
/**
* @var string
*/
protected $currentChannel = 'null';
/**
* @param string $currentChannel
*/
public function __construct($currentChannel)
{
$this->currentChannel = $currentChannel;
}
/**
* @param ChannelInterface $channel
*/
public function addChannel(ChannelInterface $channel)
{
$this->channels[$channel->getName()] = $channel;
}
/**
* @param CollectorInterface $collector
*/
public function addCollector(CollectorInterface $collector)
{
$this->collectors[] = $collector;
}
/**
* Select current channel.
*
* @param $name
* @return $this
*/
public function channel($name)
{
if (!isset($this->channels[$name])) {
throw new \InvalidArgumentException(sprintf('Notification channel "%s" is not defined.', $name));
}
$this->currentChannel = $name;
return $this;
}
/**
* @return ChannelInterface[]
*/
public function getChannels()
{
return $this->channels;
}
/**
* @return Notification[][]
*/
public function getNotifications()
{
$notifications = $this->collectNotifications();
foreach ($this->channels as $channel) {
$channelName = $channel->getName();
if (!array_key_exists($channelName, $notifications)) {
$notifications[$channelName] = [];
}
$notifications[$channelName] = array_merge($notifications[$channelName], $channel->getNotifications());
}
return $notifications;
}
/**
* @param string $type
* @param string $message
* @param string $title
* @param array $options
*/
public function addNotification($type, $message, $title = null, $options = [])
{
if (!isset($this->channels[$this->currentChannel])) {
throw new \InvalidArgumentException(
sprintf('Notification channel "%s" is not defined.', $this->currentChannel)
);
}
$this->channels[$this->currentChannel]->addNotification($type, $message, $title, $options);
}
/**
* Collect all notifications from all collectors.
*
* @return array
*/
protected function collectNotifications()
{
$notifications = [];
foreach ($this->collectors as $collector) {
$channelName = $collector->getChannel();
if (!array_key_exists($channelName, $notifications)) {
$notifications[$channelName] = [];
}
$notifications[$channelName] = array_merge($notifications[$channelName], $collector->collect());
}
return $notifications;
}
}