|
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
|
|
|
* Message priorities. |
|
77
|
|
|
*/ |
|
78
|
|
|
const LOWEST_PRIORITY = -2; |
|
79
|
|
|
const LOW_PRIORITY = -1; |
|
80
|
|
|
const NORMAL_PRIORITY = 0; |
|
81
|
|
|
const HIGH_PRIORITY = 1; |
|
82
|
|
|
const EMERGENCY_PRIORITY = 2; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $content |
|
86
|
|
|
* |
|
87
|
|
|
* @return static |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function create($content = '') |
|
90
|
|
|
{ |
|
91
|
|
|
return new static($content); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $content |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __construct($content = '') |
|
98
|
|
|
{ |
|
99
|
|
|
$this->content = $content; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set the content of the Pushover message. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $content |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
|
|
public function content($content) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->content = $content; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set the title of the Pushover message. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $title |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
|
|
public function title($title) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->title = $title; |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the time of the Pushover message. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Carbon|int $time |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function time($time) |
|
135
|
|
|
{ |
|
136
|
|
|
if ($time instanceof Carbon) { |
|
137
|
|
|
$time = $time->timestamp; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->timestamp = $time; |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Set a supplementary url for the Pushover message. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $url |
|
149
|
|
|
* @param string $title |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function url($url, $title = null) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->url = $url; |
|
155
|
|
|
$this->urlTitle = $title; |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set the sound of the Pushover message. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $sound |
|
164
|
|
|
* @return $this |
|
165
|
|
|
*/ |
|
166
|
|
|
public function sound($sound) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->sound = $sound; |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Set the priority of the Pushover message. |
|
175
|
|
|
* Retry and expire are mandatory when setting the priority to emergency. |
|
176
|
|
|
* |
|
177
|
|
|
* @param int $priority |
|
178
|
|
|
* @param int $retryTimeout |
|
179
|
|
|
* @param int $expireAfter |
|
180
|
|
|
* @return $this |
|
181
|
|
|
*/ |
|
182
|
|
|
public function priority($priority, $retryTimeout = null, $expireAfter = null) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->noEmergencyWithoutRetryOrExpire($priority, $retryTimeout, $expireAfter); |
|
185
|
|
|
|
|
186
|
|
|
$this->priority = $priority; |
|
187
|
|
|
$this->retry = $retryTimeout; |
|
188
|
|
|
$this->expire = $expireAfter; |
|
189
|
|
|
|
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set the priority of the Pushover message to the lowest priority. |
|
195
|
|
|
* |
|
196
|
|
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function lowestPriority() |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->priority(self::LOWEST_PRIORITY); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Set the priority of the Pushover message to low. |
|
205
|
|
|
* |
|
206
|
|
|
* @return $this |
|
207
|
|
|
*/ |
|
208
|
|
|
public function lowPriority() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->priority(self::LOW_PRIORITY); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Set the priority of the Pushover message to normal. |
|
215
|
|
|
* |
|
216
|
|
|
* @return $this |
|
217
|
|
|
*/ |
|
218
|
|
|
public function normalPriority() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->priority(self::NORMAL_PRIORITY); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Set the priority of the Pushover message to high. |
|
225
|
|
|
* |
|
226
|
|
|
* @return $this |
|
227
|
|
|
*/ |
|
228
|
|
|
public function highPriority() |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->priority(self::HIGH_PRIORITY); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Set the priority of the Pushover message to emergency. |
|
235
|
|
|
* Retry and expire are mandatory when setting the priority to emergency. |
|
236
|
|
|
* |
|
237
|
|
|
* @param int $retryTimeout |
|
238
|
|
|
* @param int $expireAfter |
|
239
|
|
|
* @return $this |
|
240
|
|
|
*/ |
|
241
|
|
|
public function emergencyPriority($retryTimeout, $expireAfter) |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->priority(self::EMERGENCY_PRIORITY, $retryTimeout, $expireAfter); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Array representation of Pushover Message. |
|
248
|
|
|
* |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
public function toArray() |
|
252
|
|
|
{ |
|
253
|
|
|
return [ |
|
254
|
|
|
'message' => $this->content, |
|
255
|
|
|
'title' => $this->title, |
|
256
|
|
|
'timestamp' => $this->timestamp, |
|
257
|
|
|
'priority' => $this->priority, |
|
258
|
|
|
'url' => $this->url, |
|
259
|
|
|
'url_title' => $this->urlTitle, |
|
260
|
|
|
'sound' => $this->sound, |
|
261
|
|
|
'retry' => $this->retry, |
|
262
|
|
|
'expire' => $this->expire, |
|
263
|
|
|
]; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Ensure an emergency message has an retry and expiry time. |
|
268
|
|
|
* |
|
269
|
|
|
* @param int $priority |
|
270
|
|
|
* @param int $retry |
|
271
|
|
|
* @param int $expire |
|
272
|
|
|
* @throws EmergencyNotificationRequiresRetryAndExpire |
|
273
|
|
|
*/ |
|
274
|
|
|
protected function noEmergencyWithoutRetryOrExpire($priority, $retry, $expire) |
|
275
|
|
|
{ |
|
276
|
|
|
if ($priority == self::EMERGENCY_PRIORITY && (! isset($retry) || ! isset($expire))) { |
|
277
|
|
|
throw new EmergencyNotificationRequiresRetryAndExpire(); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|