-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathNotification.php
275 lines (239 loc) · 6.05 KB
/
Notification.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace webzop\notifications;
use webzop\notifications\model\Notifications;
use Yii;
use yii\base\InvalidConfigException;
/**
* This is the base class for a notification.
*
* @property string $key
* @property integer $userId
* @property array $data
*/
abstract class Notification extends \yii\base\BaseObject
{
public $key;
public $userId = 0;
public $data = [];
/**
* @var string|null
*/
public $tag = null;
/**
* @var string|null
*/
public $priority = null;
/**
* @var string|null
*/
public $ttl = null;
const PRIORITY_LOWEST = 'very-low';
const PRIORITY_LOW = 'low';
const PRIORITY_NORMAL = 'normal';
const PRIORITY_HIGH = 'high';
const DEFAULT_TTL = 7200; // [in seconds]. 7200 = valid for two hours
/**
* Time that has to pass until a notification with same user and key con be sent again.
* It has to be a string that can be passed to @see \DateInterval (@link http://php.net/manual/en/dateinterval.construct.php)
* If FALSE, this control is disabled
* @var string
*/
protected $renotification_time = FALSE;
/**
* Create an instance
*
* @param string $key
* @param array $params notification properties
* @return static the newly created Notification
* @throws \Exception
*/
public static function create($key, $params = []){
$params['key'] = $key;
return new static($params);
}
/**
* Determines if the notification can be sent.
*
* @param Channel $channel
* @return bool
* @throws \Exception
*/
public function shouldSend($channel)
{
// If the renotification_time params is false we don't need to check the interval
if (empty($this->renotification_time)) {
return TRUE;
}
// Workaround:
// After the notification on the screen channel, the next are not sent because it finds the one just sent.
// Adds 1 second to solve this problem.
$margin = static::getLimit('PT1S')->getTimestamp();
// The notification can be sent if there aren't others with same user/key sent in the period specified in
// renotification_time params
$end = static::getLimit($this->renotification_time)->getTimestamp();
$className = $this->className();
$notifications = Notifications::find()
->andWhere([
'user_id' => $this->userId,
'key' => $this->key,
'class' => strtolower(substr($className, strrpos($className, '\\')+1, -12)),
])
->andWhere(['>', 'created_at', $end])
->andWhere(['<', 'created_at', $margin])
->exists();
return !$notifications;
}
/**
* Gets the notification title
*
* @return string
*/
abstract public function getTitle();
/**
* Gets the notification description
*
* @return string|null
*/
public function getDescription(){
return null;
}
/**
* Gets the notification route
*
* @return array|null
*/
public function getRoute(){
return null;
}
/**
* Gets notification data
*
* @return array
*/
public function getData(){
return $this->data;
}
/**
* Sets notification data
*
* @param array $data
* @return self
*/
public function setData($data = []){
$this->data = $data;
return $this;
}
/**
* Gets notification tag
*
* @return string|null
*/
public function getTag(){
return $this->tag;
}
/**
* Sets notification tag
*
* @param string|null $tag
* @return self
*/
public function setTag($tag = null){
$this->tag = $tag;
return $this;
}
/**
* Sets notification priority
*
* @param string $priority
* @return self
*/
public function setPriority($priority){
$this->priority = $priority;
return $this;
}
/**
* Gets notification priority
*
* @return string
*/
public function getPriority(){
if($this->priority) {
return $this->priority;
}
return Notification::PRIORITY_NORMAL;
}
/**
* Sets notification TTL
*
* @param string $ttl
* @return self
*/
public function setTTL($ttl){
$this->ttl = $ttl;
return $this;
}
/**
* Gets notification TTL
*
* @return string
*/
public function getTTL(){
if($this->ttl) {
return $this->ttl;
}
return Notification::DEFAULT_TTL;
}
/**
* Gets the UserId
*
* @return int
*/
public function getUserId(){
return $this->userId;
}
/**
* Sets the UserId
*
* @param int $id
* @return self
*/
public function setUserId($id){
$this->userId = $id;
return $this;
}
/**
* Sends this notification to all channels
*
*/
public function send(){
$module = Yii::$app->getModule('notifications');
if(is_null($module)) {
throw new InvalidConfigException("Please set up the module in the web/console settings, see README for instructions");
}
$module->send($this);
}
/**
* Calculate a time limit subtractive the interval to the current moment
* @param string $time interval, string passed to the constructor of \DateInterval
* @return \DateTime
* @throws \Exception
*/
public static function getLimit($time)
{
return (new \DateTime())->sub(new \DateInterval($time));
}
/**
* @return string
*/
public function getRenotificationTime(): string
{
return $this->renotification_time;
}
/**
* @param string $renotification_time
*/
public function setRenotificationTime(string $renotification_time): void
{
$this->renotification_time = $renotification_time;
}
}