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 (#17)
by Luca
04:02 queued 02:00
created

ApnChannel   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 87.76%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
c 4
b 0
f 0
lcom 1
cbo 9
dl 0
loc 119
ccs 43
cts 49
cp 0.8776
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A openConnection() 0 10 2
A __construct() 0 8 1
C send() 0 48 8
A closeConnection() 0 4 1
1
<?php
2
3
namespace NotificationChannels\Apn;
4
5
use Exception;
6
use Illuminate\Events\Dispatcher;
7
use Illuminate\Notifications\Events\NotificationFailed;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Apn\Exceptions\SendingFailed;
10
use ZendService\Apple\Apns\Client\Message as Client;
11
use ZendService\Apple\Apns\Message as Packet;
12
use ZendService\Apple\Apns\Message\Alert;
13
use ZendService\Apple\Apns\Response\Message as Response;
14
15
class ApnChannel
16
{
17
    const SANDBOX = 0;
18
    const PRODUCTION = 1;
19
20
    /** @var string */
21
    protected $environment;
22
23
    /** @var string */
24
    protected $certificate;
25
26
    /** @var string|null */
27
    protected $passPhrase;
28
29
    /** @var \ZendService\Apple\Apns\Client\Message */
30
    protected $client;
31
32
    /** @var \Illuminate\Events\Dispatcher */
33
    protected $events;
34
35
    /**
36
     * @param \ZendService\Apple\Apns\Client\Message $client
37
     * @param \Illuminate\Events\Dispatcher $events
38
     * @param string $environment
39
     * @param string $certificate
40
     * @param string|null $passPhrase
41
     */
42 1
    public function __construct(Client $client, Dispatcher $events, $environment, $certificate, $passPhrase = null)
43
    {
44 1
        $this->client = $client;
45 1
        $this->events = $events;
46 1
        $this->environment = $environment;
47 1
        $this->certificate = $certificate;
48 1
        $this->passPhrase = $passPhrase;
49 1
    }
50
51
    /**
52
     * Send the notification to Apple Push Notification Service.
53
     *
54
     * @param mixed $notifiable
55
     * @param \Illuminate\Notifications\Notification $notification
56
     *
57
     * @throws \NotificationChannels\Apn\Exceptions\SendingFailed
58
     */
59 1
    public function send($notifiable, Notification $notification)
60
    {
61 1
        $devices = (array) $notifiable->routeNotificationFor('apn');
62 1
        if (! $devices) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $devices of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
            return;
64
        }
65
66 1
        if (! $this->openConnection()) {
67
            return;
68
        }
69
70 1
        foreach ($devices as $device) {
71
            try {
72 1
                $deviceToken = ($device instanceof ApnDeviceInterface) ? $device->getToken() : $device;
73
74 1
                $message = $notification->toApn($notifiable, $device);
75 1
                if (! $message) {
76
                    continue;
77
                }
78
79 1
                $alert = new Alert();
80 1
                $alert->setTitle($message->title);
81 1
                $alert->setBody($message->body);
82
83 1
                $packet = new Packet();
84 1
                $packet->setToken($deviceToken);
85 1
                $packet->setBadge($message->badge);
86 1
                $packet->setSound($message->sound);
87 1
                $packet->setAlert($alert);
88 1
                $packet->setCustom($message->custom);
89
90 1
                $response = $this->client->send($packet);
91
92 1
                if ($response->getCode() !== Response::RESULT_OK) {
93 1
                    $this->events->fire(
94 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...
95 1
                            'token' => $deviceToken,
96 1
                            'error' => $response->getCode(),
97 1
                        ])
98 1
                    );
99 1
                }
100 1
            } catch (Exception $e) {
101
                throw SendingFailed::create($e);
102
            }
103 1
        }
104
105 1
        $this->closeConnection();
106 1
    }
107
108
    /**
109
     * Open the connection.
110
     *
111
     * @return bool
112
     *
113
     * @throws \NotificationChannels\Apn\Exceptions\ConnectionFailed
114
     */
115 1
    private function openConnection()
116
    {
117
        try {
118 1
            $this->client->open($this->environment, $this->certificate, $this->passPhrase);
119
120 1
            return true;
121
        } catch (Exception $exception) {
122
            throw Exceptions\ConnectionFailed::create($exception);
123
        }
124
    }
125
126
    /**
127
     * Close the connection.
128
     */
129 1
    private function closeConnection()
130
    {
131 1
        $this->client->close();
132 1
    }
133
}
134