WebPushChannel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 63
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 23 2
B deleteInvalidSubscriptions() 0 12 5
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();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
50
        //$this->deleteInvalidSubscriptions($response, $subscriptions);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
    }
52
53
    /**
54
     * @param  array|bool $response
55
     * @param  \Illuminate\Database\Eloquent\Collection $subscriptions
56
     * @return void
57
     */
58
    protected function deleteInvalidSubscriptions($response, $subscriptions)
59
    {
60
        if (! is_array($response)) {
61
            return;
62
        }
63
64
        foreach ($response as $index => $value) {
65
            if (! $value['success'] && isset($subscriptions[$index])) {
66
                $subscriptions[$index]->delete();
67
            }
68
        }
69
    }
70
}
71