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 ( da739e...4625ef )
by Mohamed
02:02
created

WebPushChannel::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 13
nc 2
nop 2
1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Minishlink\WebPush\WebPush;
6
use Illuminate\Notifications\Notification;
7
8
class WebPushChannel
9
{
10
    /** @var \Minishlink\WebPush\WebPush */
11
    protected $webPush;
12
13
    /**
14
     * @param  \Minishlink\WebPush\WebPush $webPush
15
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
16
     */
17
    public function __construct(WebPush $webPush)
18
    {
19
        $this->webPush = $webPush;
20
    }
21
22
    /**
23
     * Send the given notification.
24
     *
25
     * @param  mixed $notifiable
26
     * @param  \Illuminate\Notifications\Notification $notification
27
     * @return void
28
     */
29
    public function send($notifiable, Notification $notification)
30
    {
31
        $subscriptions = $notifiable->routeNotificationFor('WebPush');
32
33
        if ($subscriptions->isEmpty()) {
34
            return;
35
        }
36
37
        $payload = json_encode($notification->toWebPush($notifiable, $notification)->toArray());
0 ignored issues
show
Bug introduced by
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...
38
39
        $subscriptions->each(function ($sub) use ($payload) {
40
            $this->webPush->sendNotification(
41
                $sub->endpoint,
42
                $payload,
43
                $sub->public_key,
44
                $sub->auth_token
45
            );
46
        });
47
48
        $response = $this->webPush->flush();
49
50
        $this->deleteInvalidSubscriptions($response, $subscriptions);
51
    }
52
53
    /**
54
     * @param array|bool $response
55
     * @param \Illuminate\Database\Eloquent\Collection $subscriptions
56
     */
57
    protected function deleteInvalidSubscriptions($response, $subscriptions)
58
    {
59
        if (! is_array($response)) {
60
            return;
61
        }
62
63
        foreach ($response as $index => $value) {
64
            if (! $value['success'] && isset($subscriptions[$index])) {
65
                $subscriptions[$index]->delete();
66
            }
67
        }
68
    }
69
}
70