1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushover; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
7
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
8
|
|
|
use NotificationChannels\Pushover\Exceptions\ServiceCommunicationError; |
9
|
|
|
|
10
|
|
|
class PushoverChannel |
11
|
|
|
{ |
12
|
|
|
/** @var Pushover */ |
13
|
|
|
protected $pushover; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create a new Pushover channel instance. |
17
|
|
|
* |
18
|
|
|
* @param Pushover $pushover |
19
|
|
|
*/ |
20
|
4 |
|
public function __construct(Pushover $pushover, Dispatcher $events) |
21
|
|
|
{ |
22
|
4 |
|
$this->pushover = $pushover; |
23
|
4 |
|
$this->events = $events; |
|
|
|
|
24
|
4 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Send the given notification. |
28
|
|
|
* |
29
|
|
|
* @param mixed $notifiable |
30
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
31
|
|
|
* |
32
|
|
|
* @throws \NotificationChannels\Pushover\Exceptions\CouldNotSendNotification |
33
|
|
|
*/ |
34
|
4 |
|
public function send($notifiable, Notification $notification) |
35
|
|
|
{ |
36
|
4 |
|
if (! $pushoverKey = $notifiable->routeNotificationFor('pushover')) { |
37
|
1 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$message = $notification->toPushover($notifiable); |
|
|
|
|
41
|
|
|
|
42
|
|
|
try { |
43
|
3 |
|
$this->pushover->send([ |
44
|
3 |
|
'user' => $pushoverKey, |
45
|
3 |
|
'message' => $message->content, |
46
|
3 |
|
'title' => $message->title, |
47
|
3 |
|
'timestamp' => $message->timestamp, |
48
|
3 |
|
'priority' => $message->priority, |
49
|
3 |
|
'url' => $message->url, |
50
|
3 |
|
'url_title' => $message->urlTitle, |
51
|
3 |
|
'sound' => $message->sound, |
52
|
3 |
|
'retry' => $message->retry, |
53
|
3 |
|
'expire' => $message->expire, |
54
|
3 |
|
]); |
55
|
3 |
|
} catch (ServiceCommunicationError $serviceCommunicationError) { |
56
|
1 |
|
$this->events->fire( |
57
|
1 |
|
new NotificationFailed($notifiable, $notification, 'pushover', [ |
58
|
1 |
|
$serviceCommunicationError->getMessage() |
59
|
1 |
|
]) |
60
|
1 |
|
); |
61
|
|
|
} |
62
|
3 |
|
} |
63
|
|
|
} |
64
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: