1
|
|
|
<?php namespace Cornford\Notifier\Models; |
2
|
|
|
|
3
|
|
|
use Cornford\Notifier\Contracts\NotifierNotificationInterface; |
4
|
|
|
use Cornford\Notifier\Exceptions\NotifierNotificationArgumentException; |
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class Notification implements NotifierNotificationInterface { |
9
|
|
|
|
10
|
|
|
const NOTIFICATION_TYPE_NONE = 'message'; |
11
|
|
|
const NOTIFICATION_TYPE_INFO = 'info'; |
12
|
|
|
const NOTIFICATION_TYPE_SUCCESS = 'success'; |
13
|
|
|
const NOTIFICATION_TYPE_WARNING = 'warn'; |
14
|
|
|
const NOTIFICATION_TYPE_DANGER = 'error'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Options. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $options = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Notification id. |
25
|
|
|
* |
26
|
|
|
* @var integer |
27
|
|
|
*/ |
28
|
|
|
protected $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Notification message. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $message; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Notification message type. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $type; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Notification message date time. |
46
|
|
|
* |
47
|
|
|
* @var DateTime |
48
|
|
|
*/ |
49
|
|
|
protected $datetime; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Notification message expiry in minutes. |
53
|
|
|
* |
54
|
|
|
* @var DateTime|integer |
55
|
|
|
*/ |
56
|
|
|
protected $expiry; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Notification message displayed status. |
60
|
|
|
* |
61
|
|
|
* @var boolean |
62
|
|
|
*/ |
63
|
|
|
protected $displayed = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
* |
68
|
|
|
* @param integer $id |
69
|
|
|
* @param string $message |
70
|
|
|
* @param string $type |
71
|
|
|
* @param DateTime $datetime |
72
|
|
|
* @param DateTime|integer $expiry |
73
|
|
|
* @param array $options |
74
|
|
|
* |
75
|
|
|
* @throws NotifierNotificationArgumentException |
76
|
|
|
*/ |
77
|
|
|
public function __construct($id, $message, $type, $datetime, $expiry = 0, array $options = []) |
78
|
|
|
{ |
79
|
|
|
if (empty($id) || !is_numeric($id)) { |
80
|
|
|
throw new NotifierNotificationArgumentException('Invalid id supplied.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (empty($message) || !is_string($message)) { |
84
|
|
|
throw new NotifierNotificationArgumentException('Invalid message supplied.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (empty($message) || |
88
|
|
|
!in_array( |
89
|
|
|
$type, |
90
|
|
|
[ |
91
|
|
|
self::NOTIFICATION_TYPE_NONE, |
92
|
|
|
self::NOTIFICATION_TYPE_INFO, |
93
|
|
|
self::NOTIFICATION_TYPE_SUCCESS, |
94
|
|
|
self::NOTIFICATION_TYPE_WARNING, |
95
|
|
|
self::NOTIFICATION_TYPE_DANGER |
96
|
|
|
] |
97
|
|
|
) |
98
|
|
|
) { |
99
|
|
|
throw new NotifierNotificationArgumentException('Invalid message type supplied.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$datetime instanceof DateTime) { |
103
|
|
|
throw new NotifierNotificationArgumentException('Invalid message date time supplied.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!is_numeric($expiry) && !$expiry instanceof DateTime) { |
107
|
|
|
throw new NotifierNotificationArgumentException('Invalid message expiry supplied.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->setId($id); |
111
|
|
|
$this->setMessage($message); |
112
|
|
|
$this->setType($type); |
113
|
|
|
$this->setDatetime($datetime); |
114
|
|
|
$this->setExpiry($expiry); |
115
|
|
|
$this->setOptions($options); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the notification id. |
120
|
|
|
* |
121
|
|
|
* @param integer $value |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function setId($value) |
126
|
|
|
{ |
127
|
|
|
$this->id = $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the notification id. |
132
|
|
|
* |
133
|
|
|
* @return integer |
134
|
|
|
*/ |
135
|
|
|
public function getId() |
136
|
|
|
{ |
137
|
|
|
return $this->id; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the notification message. |
142
|
|
|
* |
143
|
|
|
* @param string $value |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function setMessage($value) |
148
|
|
|
{ |
149
|
|
|
$this->message = $value; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the notification message. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getMessage() |
158
|
|
|
{ |
159
|
|
|
return $this->message; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the notification message type. |
164
|
|
|
* |
165
|
|
|
* @param string $value |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public function setType($value) |
170
|
|
|
{ |
171
|
|
|
$this->type = $value; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the notification message. |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getType() |
180
|
|
|
{ |
181
|
|
|
return $this->type; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set the notification message date time. |
186
|
|
|
* |
187
|
|
|
* @param DateTime $value |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function setDatetime($value) |
192
|
|
|
{ |
193
|
|
|
$this->datetime = $value; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the notification message date time. |
198
|
|
|
* |
199
|
|
|
* @return DateTime |
200
|
|
|
*/ |
201
|
|
|
public function getDatetime() |
202
|
|
|
{ |
203
|
|
|
return $this->datetime; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set the notification message expiry in minutes or with a DateTime object. |
208
|
|
|
* |
209
|
|
|
* @param DateTime|integer $value |
210
|
|
|
* |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function setExpiry($value) |
214
|
|
|
{ |
215
|
|
|
$this->expiry = $value; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get the notification message expiry in minutes or a DateTime object. |
220
|
|
|
* |
221
|
|
|
* @return DateTime|integer |
222
|
|
|
*/ |
223
|
|
|
public function getExpiry() |
224
|
|
|
{ |
225
|
|
|
return $this->expiry; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Calculate the number of minutes between now and the given duration. |
230
|
|
|
* |
231
|
|
|
* @param DateTime|integer $duration |
232
|
|
|
* |
233
|
|
|
* @return integer |
234
|
|
|
*/ |
235
|
|
|
protected function getMinutes($duration) |
236
|
|
|
{ |
237
|
|
|
if ($duration instanceof DateTime) { |
238
|
|
|
$dateNow = new DateTime('now'); |
239
|
|
|
|
240
|
|
|
if (($duration->format('U')) < ($dateNow->format('U'))) { |
241
|
|
|
return 0; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$interval = $dateNow->diff($duration); |
245
|
|
|
|
246
|
|
|
return $interval->format('%i'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $duration; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Is notification message expired. |
254
|
|
|
* |
255
|
|
|
* @return boolean |
256
|
|
|
*/ |
257
|
|
|
public function isExpired() |
258
|
|
|
{ |
259
|
|
|
$expiryInSeconds = ($this->getMinutes($this->getExpiry()) * 60); |
260
|
|
|
|
261
|
|
|
if (($this->getDatetime()->add(new DateInterval('PT' . $expiryInSeconds . 'S'))->format('U')) > ((new DateTime('now'))->format('U'))) { |
262
|
|
|
return false; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return true; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Set the notification message displayed status. |
270
|
|
|
* |
271
|
|
|
* @param boolean $value |
272
|
|
|
* |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
public function setDisplayed($value) |
276
|
|
|
{ |
277
|
|
|
$this->displayed = $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get the notification message displayed status. |
282
|
|
|
* |
283
|
|
|
* @return boolean |
284
|
|
|
*/ |
285
|
|
|
public function getDisplayed() |
286
|
|
|
{ |
287
|
|
|
return $this->displayed; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get the notification message displayed status. |
292
|
|
|
* |
293
|
|
|
* @return boolean |
294
|
|
|
*/ |
295
|
|
|
public function isDisplayed() |
296
|
|
|
{ |
297
|
|
|
return $this->displayed; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set the notification message options. |
302
|
|
|
* |
303
|
|
|
* @param array $value |
304
|
|
|
* |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function setOptions(array $value) |
308
|
|
|
{ |
309
|
|
|
$this->options = $value; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get the notification message options. |
314
|
|
|
* |
315
|
|
|
* @return array |
316
|
|
|
*/ |
317
|
|
|
public function getOptions() |
318
|
|
|
{ |
319
|
|
|
return $this->options; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* To array method. |
324
|
|
|
* |
325
|
|
|
* @return array |
326
|
|
|
*/ |
327
|
|
|
public function __toArray() |
328
|
|
|
{ |
329
|
|
|
return [ |
330
|
|
|
'id' => $this->getId(), |
331
|
|
|
'message' => $this->getMessage(), |
332
|
|
|
'options' => $this->getOptions(), |
333
|
|
|
'type' => $this->getType(), |
334
|
|
|
'datetime' => $this->getDatetime(), |
335
|
|
|
'expiry' => $this->getExpiry(), |
336
|
|
|
'displayed' => $this->getDisplayed(), |
337
|
|
|
'expired' => $this->isExpired(), |
338
|
|
|
]; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
} |