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 ( 466e5a...bfa63f )
by Dwight
03:16
created

ApnChannel::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 8
cts 9
cp 0.8889
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0054
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 APNS client.
15
     *
16
     * @var \Pushok\Client
17
     */
18
    protected $client;
19
20
    /**
21
     * The event dispatcher.
22
     *
23
     * @var \Illuminate\Contracts\Events\Dispatcher
24
     */
25
    protected $events;
26
27
    /**
28
     * Create a new channel instance.
29
     *
30
     * @param  \Pushok\Client  $client
31
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
32
     */
33 6
    public function __construct(Client $client, Dispatcher $events)
34
    {
35 6
        $this->client = $client;
36 6
        $this->events = $events;
37 6
    }
38
39
    /**
40
     * Send the notification to Apple Push Notification Service.
41
     *
42
     * @param mixed $notifiable
43
     * @param \Illuminate\Notifications\Notification $notification
44
     * @return array|void
45
     */
46 3 View Code Duplication
    public function send($notifiable, Notification $notification)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 3
        $tokens = (array) $notifiable->routeNotificationFor('apn', $notification);
49
50 3
        if (empty($tokens)) {
51
            return;
52
        }
53
54 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...
55
56 3
        $responses = $this->sendNotifications(
57 3
            $message->client ?? $this->client,
58
            $message,
59
            $tokens
60
        );
61
62 3
        $this->dispatchEvents($notifiable, $notification, $responses);
63
64 3
        return $responses;
65
    }
66
67
    /**
68
     * Send the message to the given tokens through the given client.
69
     *
70
     * @param  \Pushok\Client  $client
71
     * @param  \NotificationChannels\Apn\ApnMessage  $message
72
     * @param  array  $tokens
73
     * @return array
74
     */
75 6
    protected function sendNotifications(Client $client, ApnMessage $message, array $tokens)
76
    {
77 6
        foreach ($tokens as $token) {
78 6
            $client->addNotification((new ApnAdapter)->adapt($message, $token));
79
        }
80
81 6
        return $client->push();
82
    }
83
84
    /**
85
     * Dispatch failed events for notifications that weren't delivered.
86
     *
87
     * @param mixed $notifiable
88
     * @param \Illuminate\Notifications\Notification $notification
89
     * @param array $responses
90
     * @return void
91
     */
92 6
    protected function dispatchEvents($notifiable, $notification, array $responses)
93
    {
94 6
        foreach ($responses as $response) {
95 2
            if ($response->getStatusCode() === Response::APNS_SUCCESS) {
96 2
                continue;
97
            }
98
99 2
            $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...
100 2
                'id' => $response->getApnsId(),
101 2
                'token' => $response->getDeviceToken(),
102 2
                'error' => $response->getErrorReason(),
103
            ]);
104
105 2
            $this->events->dispatch($event);
106
        }
107 6
    }
108
}
109