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
Pull Request — master (#75)
by
unknown
03:05
created

WebPushChannel   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 85
ccs 28
cts 38
cp 0.7368
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 25 3
A deleteInvalidSubscriptions() 0 12 5
B logErrorsInDebug() 0 19 9
1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Illuminate\Support\Facades\Log;
6
use Minishlink\WebPush\WebPush;
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 2
    public function __construct(WebPush $webPush)
19
    {
20 2
        $this->webPush = $webPush;
21 2
    }
22
23
    /**
24
     * Send the given notification.
25
     *
26
     * @param  mixed $notifiable
27
     * @param  \Illuminate\Notifications\Notification $notification
28
     * @return void
29
     */
30 2
    public function send($notifiable, Notification $notification)
31
    {
32 2
        $subscriptions = $notifiable->routeNotificationFor('WebPush');
33
34 2
        if (! $subscriptions || $subscriptions->isEmpty()) {
35
            return;
36
        }
37
38 2
        $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...
39
40 2
        $subscriptions->each(function ($sub) use ($payload) {
41 2
            $this->webPush->sendNotification(
42 2
                $sub->endpoint,
43 2
                $payload,
44 2
                $sub->public_key,
45 2
                $sub->auth_token
46
            );
47 2
        });
48
49 2
        $response = $this->webPush->flush();
50
51 2
        $this->logErrorsInDebug($response, $subscriptions, $payload);
52
53 2
        $this->deleteInvalidSubscriptions($response, $subscriptions);
54 2
    }
55
56
    /**
57
     * @param  array|bool $response
58
     * @param  \Illuminate\Database\Eloquent\Collection $subscriptions
59
     * @return void
60
     */
61 2
    protected function deleteInvalidSubscriptions($response, $subscriptions)
62
    {
63 2
        if (! is_array($response)) {
64 1
            return;
65
        }
66
67 1
        foreach ($response as $index => $value) {
68 1
            if (! $value['success'] && isset($subscriptions[$index])) {
69 1
                $subscriptions[$index]->delete();
70
            }
71
        }
72 1
    }
73
74 2
    protected function logErrorsInDebug($response, $subscriptions, $payload)
75
    {
76 2
		if(config('webpush.enable_logging')){
77
			if(is_array($response)){
78
				foreach($response as $index => $push){
79
					if(!$push['success']){
80
						Log::error("[WebPush] Error pushing: {$push['message']}\nEndpoint: {$push['endpoint']}\nPayload: {$payload}");
81
					}else{
82
						Log::info("[WebPush] Push successful\nEndpoint: {$subscriptions[$index]->endpoint}");
83
					}
84
				}
85
			}elseif(is_bool($response) && $response === true){
86
				Log::info('[WebPush] All messages successfully pushed');
87
			}elseif(is_bool($response) && $response === false){
88
				Log::info('[WebPush] No notifications in the queue');
89
			}
90
91
		}
92 2
    }
93
}
94