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 ( 6e8b8a...3f3148 )
by Cretu
03:22
created

WebPushChannel::send()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0015

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 17
cts 18
cp 0.9444
rs 9.504
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3.0015
1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Minishlink\WebPush\WebPush;
6
use Minishlink\WebPush\Subscription;
7
use Illuminate\Notifications\Notification;
8
9
class WebPushChannel
10
{
11
    /** @var \Minishlink\WebPush\WebPush */
12
    protected $webPush;
13
14
    /**
15
     * @param  \Minishlink\WebPush\WebPush $webPush
16
     * @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...
17
     */
18 3
    public function __construct(WebPush $webPush)
19
    {
20 3
        $this->webPush = $webPush;
21 3
    }
22
23
    /**
24
     * Send the given notification.
25
     *
26
     * @param  mixed $notifiable
27
     * @param  \Illuminate\Notifications\Notification $notification
28
     * @return void
29
     */
30 3
    public function send($notifiable, Notification $notification)
31
    {
32 3
        $subscriptions = $notifiable->routeNotificationFor('WebPush');
33
34 3
        if (! $subscriptions || $subscriptions->isEmpty()) {
35
            return;
36
        }
37
38
        /** @var \NotificationChannels\WebPush\WebPushMessage $webPushMessage */
39 3
        $webPushMessage = $notification->toWebPush($notifiable, $notification);
40 3
        $options = $webPushMessage->getOptions();
41 3
        $payload = json_encode($webPushMessage->toArray());
42
43 3
        $subscriptions->each(function (PushSubscription $pushSubscription) use ($payload, $options) {
44 3
            $this->webPush->sendNotification(new Subscription(
45 3
                $pushSubscription->endpoint,
46 3
                $pushSubscription->public_key,
47 3
                $pushSubscription->auth_token,
48 3
                $pushSubscription->content_encoding
49 3
            ), $payload, false, $options);
50 3
        });
51
52 3
        $reports = $this->webPush->flush();
53
54 3
        $this->deleteInvalidSubscriptions($reports, $subscriptions);
55 3
    }
56
57
    /**
58
     * @param  \Minishlink\WebPush\MessageSentReport[] $reports
59
     * @param  \Illuminate\Database\Eloquent\Collection $subscriptions
60
     * @return void
61
     */
62 3
    protected function deleteInvalidSubscriptions($reports, $subscriptions)
63
    {
64 3
        foreach ($reports as $report) {
65 3
            if (is_null($report) || $report->isSuccess()) {
66 3
                continue;
67
            }
68
69
            /* @var \Minishlink\WebPush\MessageSentReport $report */
70 1
            $subscriptions->each(function ($subscription) use ($report) {
71 1
                if ($subscription->endpoint === $report->getEndpoint()) {
72 1
                    logger()->warning('deleting subscription cause of '.$report->getReason());
73 1
                    $subscription->delete();
74
                }
75 1
            });
76
        }
77 3
    }
78
}
79