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 ( 6dc77b...ecf0e6 )
by Dwight
13s queued 10s
created

ApnChannel::sendNotifications()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace NotificationChannels\Apn;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Notifications\Events\NotificationFailed;
7
use Illuminate\Notifications\Notification;
8
use Pushok\Client;
9
use Pushok\Response;
10
11
class ApnChannel
12
{
13
    /**
14
     * The sandbox environment identifier.
15
     *
16
     * @var int
17
     */
18
    const SANDBOX = 0;
19
20
    /**
21
     * The production environment identifier.
22
     *
23
     * @var int
24
     */
25
    const PRODUCTION = 1;
26
27
    /**
28
     * The APNS client.
29
     *
30
     * @var \Pushok\Client
31
     */
32
    protected $client;
33
34
    /**
35
     * The event dispatcher.
36
     *
37
     * @var \Illuminate\Contracts\Events\Dispatcher
38
     */
39
    protected $events;
40
41
    /**
42
     * Create a new channel instance.
43
     *
44
     * @param  \Pushok\Client  $client
45
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
46
     */
47 3
    public function __construct(Client $client, Dispatcher $events)
48
    {
49 3
        $this->client = $client;
50 3
        $this->events = $events;
51 3
    }
52
53
    /**
54
     * Send the notification to Apple Push Notification Service.
55
     *
56
     * @param mixed $notifiable
57
     * @param \Illuminate\Notifications\Notification $notification
58
     * @return array|void
59
     */
60 3
    public function send($notifiable, Notification $notification)
61
    {
62 3
        $tokens = (array) $notifiable->routeNotificationFor('apn', $notification);
63
64 3
        if (empty($tokens)) {
65
            return;
66
        }
67
68 3
        $message = $notification->toApn($notifiable);
0 ignored issues
show
Bug introduced by
The method toApn() 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...
69
70 3
        $responses = $this->sendNotifications(
71 3
            $message->client ?? $this->client,
72 3
            $message,
73 3
            $tokens
74
        );
75
76 3
        $this->dispatchEvents($notifiable, $notification, $responses);
77
78 3
        return $responses;
79
    }
80
81
    /**
82
     * Send the message to the given tokens through the given client.
83
     *
84
     * @param  \Pushok\Client  $client
85
     * @param  \NotificationChannels\Apn\ApnMessage  $message
86
     * @param  array  $tokens
87
     * @return array
88
     */
89 3
    protected function sendNotifications(Client $client, ApnMessage $message, array $tokens)
90
    {
91 3
        foreach ($tokens as $token) {
92 3
            $client->addNotification((new ApnAdapter)->adapt($message, $token));
93
        }
94
95 3
        return $client->push();
96
    }
97
98
    /**
99
     * Dispatch failed events for notifications that weren't delivered.
100
     *
101
     * @param mixed $notifiable
102
     * @param \Illuminate\Notifications\Notification $notification
103
     * @param array $responses
104
     * @return void
105
     */
106 3
    protected function dispatchEvents($notifiable, $notification, array $responses)
107
    {
108 3
        foreach ($responses as $response) {
109 1
            if ($response->getStatusCode() === Response::APNS_SUCCESS) {
110 1
                continue;
111
            }
112
113 1
            $event = new NotificationFailed($notifiable, $notification, $this, [
0 ignored issues
show
Documentation introduced by
$this is of type this<NotificationChannels\Apn\ApnChannel>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114 1
                'id' => $response->getApnsId(),
115 1
                'token' => $response->getDeviceToken(),
116 1
                'error' => $response->getErrorReason(),
117
            ]);
118
119 1
            $this->events->dispatch($event);
120
        }
121 3
    }
122
}
123