1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushover; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use NotificationChannels\Pushover\Exceptions\EmergencyNotificationRequiresRetryAndExpire; |
7
|
|
|
|
8
|
|
|
class PushoverMessage |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The text content of the message. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public $content; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The (optional) title of the message. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $title; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The (optional) timestamp of the message. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
public $timestamp; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The (optional) priority of the message. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
public $priority; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The (optional) timeout between retries when sending a message |
40
|
|
|
* with an emergency priority. The timeout is in seconds. |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
public $retry; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The (optional) expire time of a message with an emergency priority. |
48
|
|
|
* The expire time is in seconds. |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
public $expire; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The (optional) supplementary url of the message. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $url; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The (optional) supplementary url title of the message. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public $urlTitle; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The (optional) sound of the message. |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
public $sound; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set Message Type to HTML. |
77
|
|
|
* |
78
|
|
|
* @var int |
79
|
|
|
*/ |
80
|
|
|
public $html; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Message priorities. |
84
|
|
|
*/ |
85
|
|
|
const LOWEST_PRIORITY = -2; |
86
|
|
|
const LOW_PRIORITY = -1; |
87
|
|
|
const NORMAL_PRIORITY = 0; |
88
|
|
|
const HIGH_PRIORITY = 1; |
89
|
|
|
const EMERGENCY_PRIORITY = 2; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $content |
93
|
|
|
* |
94
|
|
|
* @return static |
95
|
|
|
*/ |
96
|
2 |
|
public static function create($content = '') |
97
|
|
|
{ |
98
|
2 |
|
return new static($content); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $content |
103
|
|
|
*/ |
104
|
20 |
|
public function __construct($content = '') |
105
|
|
|
{ |
106
|
20 |
|
$this->content = $content; |
107
|
20 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the content of the Pushover message. |
111
|
|
|
* |
112
|
|
|
* @param string $content |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
1 |
|
public function content($content) |
116
|
|
|
{ |
117
|
1 |
|
$this->content = $content; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the title of the Pushover message. |
124
|
|
|
* |
125
|
|
|
* @param string $title |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
3 |
|
public function title($title) |
129
|
|
|
{ |
130
|
3 |
|
$this->title = $title; |
131
|
|
|
|
132
|
3 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the time of the Pushover message. |
137
|
|
|
* |
138
|
|
|
* @param Carbon|int $time |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
4 |
|
public function time($time) |
142
|
|
|
{ |
143
|
4 |
|
if ($time instanceof Carbon) { |
144
|
1 |
|
$time = $time->timestamp; |
145
|
|
|
} |
146
|
|
|
|
147
|
4 |
|
$this->timestamp = $time; |
148
|
|
|
|
149
|
4 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set a supplementary url for the Pushover message. |
154
|
|
|
* |
155
|
|
|
* @param string $url |
156
|
|
|
* @param string $title |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
4 |
|
public function url($url, $title = null) |
160
|
|
|
{ |
161
|
4 |
|
$this->url = $url; |
162
|
4 |
|
$this->urlTitle = $title; |
163
|
|
|
|
164
|
4 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the sound of the Pushover message. |
169
|
|
|
* |
170
|
|
|
* @param string $sound |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
3 |
|
public function sound($sound) |
174
|
|
|
{ |
175
|
3 |
|
$this->sound = $sound; |
176
|
|
|
|
177
|
3 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set the priority of the Pushover message. |
182
|
|
|
* Retry and expire are mandatory when setting the priority to emergency. |
183
|
|
|
* |
184
|
|
|
* @param int $priority |
185
|
|
|
* @param int $retryTimeout |
186
|
|
|
* @param int $expireAfter |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
10 |
|
public function priority($priority, $retryTimeout = null, $expireAfter = null) |
190
|
|
|
{ |
191
|
10 |
|
$this->noEmergencyWithoutRetryOrExpire($priority, $retryTimeout, $expireAfter); |
192
|
|
|
|
193
|
9 |
|
$this->priority = $priority; |
194
|
9 |
|
$this->retry = $retryTimeout; |
195
|
9 |
|
$this->expire = $expireAfter; |
196
|
|
|
|
197
|
9 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the priority of the Pushover message to the lowest priority. |
202
|
|
|
* |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
1 |
|
public function lowestPriority() |
206
|
|
|
{ |
207
|
1 |
|
return $this->priority(self::LOWEST_PRIORITY); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the priority of the Pushover message to low. |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
1 |
|
public function lowPriority() |
216
|
|
|
{ |
217
|
1 |
|
return $this->priority(self::LOW_PRIORITY); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set the priority of the Pushover message to normal. |
222
|
|
|
* |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
1 |
|
public function normalPriority() |
226
|
|
|
{ |
227
|
1 |
|
return $this->priority(self::NORMAL_PRIORITY); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set the priority of the Pushover message to high. |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
1 |
|
public function highPriority() |
236
|
|
|
{ |
237
|
1 |
|
return $this->priority(self::HIGH_PRIORITY); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set the text type of the Pushover message to HTML. |
242
|
|
|
* |
243
|
|
|
* @return this |
244
|
|
|
*/ |
245
|
|
|
public function setHtml() |
246
|
|
|
{ |
247
|
|
|
$this->html = 1; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set the priority of the Pushover message to emergency. |
254
|
|
|
* Retry and expire are mandatory when setting the priority to emergency. |
255
|
|
|
* |
256
|
|
|
* @param int $retryTimeout |
257
|
|
|
* @param int $expireAfter |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
3 |
|
public function emergencyPriority($retryTimeout, $expireAfter) |
261
|
|
|
{ |
262
|
3 |
|
return $this->priority(self::EMERGENCY_PRIORITY, $retryTimeout, $expireAfter); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Array representation of Pushover Message. |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
2 |
|
public function toArray() |
271
|
|
|
{ |
272
|
|
|
return [ |
273
|
2 |
|
'message' => $this->content, |
274
|
2 |
|
'title' => $this->title, |
275
|
2 |
|
'timestamp' => $this->timestamp, |
276
|
2 |
|
'priority' => $this->priority, |
277
|
2 |
|
'url' => $this->url, |
278
|
2 |
|
'url_title' => $this->urlTitle, |
279
|
2 |
|
'sound' => $this->sound, |
280
|
2 |
|
'retry' => $this->retry, |
281
|
2 |
|
'expire' => $this->expire, |
282
|
2 |
|
'html' => $this->html, |
283
|
|
|
]; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Ensure an emergency message has an retry and expiry time. |
288
|
|
|
* |
289
|
|
|
* @param int $priority |
290
|
|
|
* @param int $retry |
291
|
|
|
* @param int $expire |
292
|
|
|
* @throws EmergencyNotificationRequiresRetryAndExpire |
293
|
|
|
*/ |
294
|
10 |
|
protected function noEmergencyWithoutRetryOrExpire($priority, $retry, $expire) |
295
|
|
|
{ |
296
|
10 |
|
if ($priority == self::EMERGENCY_PRIORITY && (! isset($retry) || ! isset($expire))) { |
297
|
1 |
|
throw new EmergencyNotificationRequiresRetryAndExpire(); |
298
|
|
|
} |
299
|
9 |
|
} |
300
|
|
|
} |
301
|
|
|
|