GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cdb1b9...55c78e )
by Cretu
09:06
created

src/WebPushChannel.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Minishlink\WebPush\WebPush;
6
use Illuminate\Notifications\Notification;
7
use NotificationChannels\WebPush\Events\MessageWasSent;
8
use NotificationChannels\WebPush\Events\SendingMessage;
9
10
class WebPushChannel
11
{
12
    /** @var \Minishlink\WebPush\WebPush */
13
    protected $webPush;
14
15
    /**
16
     * @param  \Minishlink\WebPush\WebPush $webPush
17
     * @return void
18
     */
19
    public function __construct(WebPush $webPush)
20
    {
21
        $this->webPush = $webPush;
22
    }
23
24
    /**
25
     * Send the given notification.
26
     *
27
     * @param  mixed $notifiable
28
     * @param  \Illuminate\Notifications\Notification $notification
29
     * @return void
30
     */
31
    public function send($notifiable, Notification $notification)
32
    {
33
        $shouldSendMessage = event(new SendingMessage($notifiable, $notification), [], true) !== false;
34
35
        if (! $shouldSendMessage) {
36
            return;
37
        }
38
39
        $subscriptions = $notifiable->routeNotificationFor('WebPush');
40
41
        if ($subscriptions->isEmpty()) {
42
            return;
43
        }
44
45
        $payload = json_encode($notification->toWebPush($notifiable, $notification)->toArray());
0 ignored issues
show
The method toWebPush() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
47
        $subscriptions->each(function ($sub) use ($payload) {
48
            $this->webPush->sendNotification(
49
                $sub->endpoint,
50
                $payload,
51
                $sub->public_key,
52
                $sub->auth_token
53
            );
54
        });
55
56
        $response = $this->webPush->flush();
57
58
        $this->deleteInvalidSubscriptions($response, $subscriptions);
59
60
        event(new MessageWasSent($notifiable, $notification));
61
    }
62
63
    /**
64
     * @param array|bool $response
65
     * @param \Illuminate\Database\Eloquent\Collection $subscriptions
66
     */
67
    protected function deleteInvalidSubscriptions($response, $subscriptions)
68
    {
69
        if (! is_array($response)) {
70
            return;
71
        }
72
73
        foreach ($response as $index => $value) {
74
            if (! $value['success'] && isset($subscriptions[$index])) {
75
                $subscriptions[$index]->delete();
76
            }
77
        }
78
    }
79
}
80