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 (#37)
by Dwight
08:34
created

ApnChannel::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 4.0961
1
<?php
2
3
namespace NotificationChannels\Apn;
4
5
use Exception;
6
use Illuminate\Events\Dispatcher;
7
use ZendService\Apple\Apns\Message\Alert;
8
use Illuminate\Notifications\Notification;
9
use ZendService\Apple\Apns\Message as Packet;
10
use ZendService\Apple\Apns\Client\Message as Client;
11
use NotificationChannels\Apn\Exceptions\SendingFailed;
12
use Illuminate\Notifications\Events\NotificationFailed;
13
use ZendService\Apple\Apns\Response\Message as Response;
14
15
class ApnChannel
16
{
17
    /**
18
     * The sandbox environment identifier.
19
     *
20
     * @var int
21
     */
22
    const SANDBOX = 0;
23
24
    /**
25
     * The production environment identifier.
26
     *
27
     * @var int
28
     */
29
    const PRODUCTION = 1;
30
31
    /** @var \ZendService\Apple\Apns\Client\Message */
32
    protected $client;
33
34
    /** @var \Illuminate\Events\Dispatcher */
35
    protected $events;
36
37
    /**
38
     * Create a new instance of the APN channel.
39
     *
40
     * @param  \ZendService\Apple\Apns\Client\Message  $client
41
     * @param  \Illuminate\Events\Dispatcher  $events
42
     */
43 2
    public function __construct(Client $client, Dispatcher $events)
44
    {
45 2
        $this->client = $client;
46 2
        $this->events = $events;
47 2
    }
48
49
    /**
50
     * Send the notification to Apple Push Notification Service.
51
     *
52
     * @param mixed $notifiable
53
     * @param \Illuminate\Notifications\Notification $notification
54
     */
55 2
    public function send($notifiable, Notification $notification)
56
    {
57 2
        $tokens = (array) $notifiable->routeNotificationFor('apn');
58 2
        if (empty($tokens)) {
59
            return;
60
        }
61
62 2
        $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...
63 2
        if (! $message) {
64
            return;
65
        }
66
67 2
        foreach ($tokens as $token) {
68 2
            $packet = $this->getPacket($message, $token);
69 2
            $this->sendPacket($notifiable, $notification, $packet, $token);
70
        }
71 2
    }
72
73
    /**
74
     * Get the packet for the given message and token.
75
     *
76
     * @param  \NotificationChannels\Apn\ApnMessage  $message
77
     * @param  string
78
     * @return \ZendService\Apple\Apns\Message
79
     */
80 2
    protected function getPacket($message, $token)
81
    {
82 2
        $alert = new Alert();
83 2
        $alert->setTitle($message->title);
84 2
        $alert->setBody($message->body);
85
86 2
        $packet = new Packet();
87 2
        $packet->setToken($token);
88 2
        $packet->setBadge($message->badge);
89 2
        $packet->setSound($message->sound);
90 2
        $packet->setCategory($message->category);
91 2
        $packet->setContentAvailable($message->contentAvailable);
92 2
        $packet->setAlert($alert);
93 2
        $packet->setCustom($message->custom);
94
95 2
        return $packet;
96
    }
97
98
    /**
99
     * Sent the notification to the given token.
100
     *
101
     * @param mixed $notifiable
102
     * @param \Illuminate\Notifications\Notification $notification
103
     * @param \ZendService\Apple\Apns\Message  $packet
104
     * @param  string  $token
105
     * @return void
106
     * @throws \NotificationChannels\Apn\Exceptions\SendingFailed
107
     */
108 2
    protected function sendPacket($notifiable, $notification, $packet, $token)
109
    {
110
        try {
111 2
            $response = $this->client->send($packet);
112
113 2
            if ($response->getCode() !== Response::RESULT_OK) {
114 1
                $this->events->fire(
115 1
                    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...
116 1
                        'token' => $token,
117 2
                        'error' => $response->getCode(),
118
                    ])
119
                );
120
            }
121
        } catch (Exception $e) {
122
            throw SendingFailed::create($e);
123
        }
124 2
    }
125
}
126