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 ( fe092f...f32e18 )
by Cretu
09:19
created

WebPushChannel::handleReports()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 2
cts 2
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
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
    /**
12
     * @var \Minishlink\WebPush\WebPush
13
     */
14
    protected $webPush;
15
16
    /**
17
     * @var \NotificationChannels\WebPush\ReportHandlerInterface
18 3
     */
19
    protected $reportHandler;
20 3
21 3
    /**
22
     * @param  \Minishlink\WebPush\WebPush $webPush
23
     * @param  \NotificationChannels\WebPush\ReportHandlerInterface $webPush
24
     * @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...
25
     */
26
    public function __construct(WebPush $webPush, ReportHandlerInterface $reportHandler)
27
    {
28
        $this->webPush = $webPush;
29
        $this->reportHandler = $reportHandler;
30 3
    }
31
32 3
    /**
33
     * Send the given notification.
34 3
     *
35
     * @param  mixed $notifiable
36
     * @param  \Illuminate\Notifications\Notification $notification
37
     * @return void
38
     */
39 3
    public function send($notifiable, Notification $notification)
40 3
    {
41 3
        /** @var \Illuminate\Database\Eloquent\Collection $subscriptions */
42
        $subscriptions = $notifiable->routeNotificationFor('WebPush');
43 3
44 3
        if (empty($subscriptions)) {
45 3
            return;
46 3
        }
47 3
48 3
        /** @var \NotificationChannels\WebPush\WebPushMessage $message */
49 3
        $message = $notification->toWebPush($notifiable, $notification);
50 3
        $payload = json_encode($message->toArray());
51
        $options = $message->getOptions();
52 3
53
        /** @var \NotificationChannels\WebPush\PushSubscription $subscription */
54 3
        foreach ($subscriptions as $subscription) {
55 3
            $this->webPush->sendNotification(new Subscription(
56
                $subscription->endpoint,
57
                $subscription->public_key,
58
                $subscription->auth_token,
59
                $subscription->content_encoding
60
            ), $payload, false, $options);
61
        }
62 3
63
        $reports = $this->webPush->flush();
64 3
65 3
        $this->handleReports($reports, $subscriptions);
66 3
    }
67
68
    /**
69
     * Handle the reports.
70 1
     *
71 1
     * @param  \Generator $reports
72 1
     * @param  \Illuminate\Database\Eloquent\Collection $subscriptions
73 1
     * @return void
74
     */
75 1
    protected function handleReports($reports, $subscriptions)
76
    {
77 3
        /** @var \Minishlink\WebPush\MessageSentReport $report */
78
        foreach ($reports as $report) {
79
            if ($report && $subscription = $this->findSubscription($subscriptions, $report)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $subscription is correct as $this->findSubscription($subscriptions, $report) (which targets NotificationChannels\Web...nel::findSubscription()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
80
                $this->reportHandler->handleReport($report, $subscription);
81
            }
82
        }
83
    }
84
85
    /**
86
     * @param \Illuminate\Database\Eloquent\Collection $subscriptions
87
     * @param \Minishlink\WebPush\MessageSentReport $report
88
     * @return void
89
     */
90
    protected function findSubscription($subscriptions, $report)
91
    {
92
        foreach ($subscriptions as $subscription) {
93
            if ($subscription->endpoint === $report->getEndpoint()) {
94
                return $subscription;
95
            }
96
        }
97
    }
98
}
99