1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\PusherPushNotifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use NotificationChannels\PusherPushNotifications\Events\MessageWasSent; |
7
|
|
|
use NotificationChannels\PusherPushNotifications\Events\SendingMessage; |
8
|
|
|
use NotificationChannels\PusherPushNotifications\Exceptions\CouldNotSendNotification; |
9
|
|
|
use Pusher; |
10
|
|
|
|
11
|
|
|
class Channel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Pusher |
15
|
|
|
*/ |
16
|
|
|
protected $pusher; |
17
|
|
|
|
18
|
|
|
public function __construct(Pusher $pusher) |
19
|
|
|
{ |
20
|
|
|
$this->pusher = $pusher; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Send the given notification. |
25
|
|
|
* |
26
|
|
|
* @param mixed $notifiable |
27
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
* @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotSendNotification |
31
|
|
|
*/ |
32
|
|
|
public function send($notifiable, Notification $notification) |
33
|
|
|
{ |
34
|
|
|
$interest = $notifiable->routeNotificationFor('PusherPushNotifications') ?: $this->interestName($notifiable); |
35
|
|
|
|
36
|
|
|
$shouldSendMessage = event(new SendingMessage($notifiable, $notification), [], true) !== false; |
37
|
|
|
|
38
|
|
|
if (!$shouldSendMessage) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$response = $this->pusher->notify( |
43
|
|
|
$interest, |
44
|
|
|
$notification->toPushNotification($notifiable)->toArray(), |
45
|
|
|
true |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
if (!in_array($response['status'], [200, 202])) { |
49
|
|
|
throw CouldNotSendNotification::pusherRespondedWithAnError($response); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
event(new MessageWasSent($notifiable, $notification)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the interest name for the notifiable. |
57
|
|
|
* |
58
|
|
|
* @param $notifiable |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
protected function interestName($notifiable) |
63
|
|
|
{ |
64
|
|
|
$class = str_replace('\\', '.', get_class($notifiable)); |
65
|
|
|
|
66
|
|
|
return $class.'.'.$notifiable->getKey(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.