1 | <?php |
||
9 | class WebPushChannel |
||
10 | { |
||
11 | /** |
||
12 | * @var \Minishlink\WebPush\WebPush |
||
13 | */ |
||
14 | protected $webPush; |
||
15 | |||
16 | /** |
||
17 | * @var \NotificationChannels\WebPush\ReportHandlerInterface |
||
18 | */ |
||
19 | protected $reportHandler; |
||
20 | |||
21 | /** |
||
22 | * @param \Minishlink\WebPush\WebPush $webPush |
||
23 | * @param \NotificationChannels\WebPush\ReportHandlerInterface $webPush |
||
24 | * @return void |
||
|
|||
25 | */ |
||
26 | 2 | public function __construct(WebPush $webPush, ReportHandlerInterface $reportHandler) |
|
31 | |||
32 | /** |
||
33 | * Send the given notification. |
||
34 | * |
||
35 | * @param mixed $notifiable |
||
36 | * @param \Illuminate\Notifications\Notification $notification |
||
37 | * @return void |
||
38 | */ |
||
39 | 2 | public function send($notifiable, Notification $notification) |
|
40 | { |
||
41 | /** @var \Illuminate\Database\Eloquent\Collection $subscriptions */ |
||
42 | 2 | $subscriptions = $notifiable->routeNotificationFor('WebPush', $notification); |
|
43 | |||
44 | 2 | if (empty($subscriptions)) { |
|
45 | return; |
||
46 | } |
||
47 | |||
48 | /** @var \NotificationChannels\WebPush\WebPushMessage $message */ |
||
49 | 2 | $message = $notification->toWebPush($notifiable, $notification); |
|
50 | 2 | $payload = json_encode($message->toArray()); |
|
51 | 2 | $options = $message->getOptions(); |
|
52 | |||
53 | /** @var \NotificationChannels\WebPush\PushSubscription $subscription */ |
||
54 | 2 | foreach ($subscriptions as $subscription) { |
|
55 | 2 | $this->webPush->queueNotification(new Subscription( |
|
56 | 2 | $subscription->endpoint, |
|
57 | 2 | $subscription->public_key, |
|
58 | 2 | $subscription->auth_token, |
|
59 | 2 | $subscription->content_encoding |
|
60 | ), $payload, $options); |
||
61 | } |
||
62 | |||
63 | 2 | $reports = $this->webPush->flush(); |
|
64 | |||
65 | 2 | $this->handleReports($reports, $subscriptions, $message); |
|
66 | 2 | } |
|
67 | |||
68 | /** |
||
69 | * Handle the reports. |
||
70 | * |
||
71 | * @param \Generator $reports |
||
72 | * @param \Illuminate\Database\Eloquent\Collection $subscriptions |
||
73 | * @param \NotificationChannels\WebPush\WebPushMessage $message |
||
74 | * @return void |
||
75 | */ |
||
76 | 2 | protected function handleReports($reports, $subscriptions, $message) |
|
85 | |||
86 | /** |
||
87 | * @param \Illuminate\Database\Eloquent\Collection $subscriptions |
||
88 | * @param \Minishlink\WebPush\MessageSentReport $report |
||
89 | * @return void |
||
90 | */ |
||
91 | 2 | protected function findSubscription($subscriptions, $report) |
|
99 | } |
||
100 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.