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 ( a365de...ed9d4f )
by Barry vd.
03:01
created

ApnChannel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 88.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 10
dl 0
loc 157
ccs 54
cts 61
cp 0.8852
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
C send() 0 47 7
A getFeedback() 0 21 3
A openConnection() 0 10 2
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\Client\Feedback as FeedbackClient;
12
use ZendService\Apple\Apns\Message as Packet;
13
use ZendService\Apple\Apns\Message\Alert;
14
use ZendService\Apple\Apns\Response\Message as Response;
15
use ZendService\Apple\Apns\Response\Feedback as FeedbackResponse;
16
17
class ApnChannel
18
{
19
    const SANDBOX = 0;
20
    const PRODUCTION = 1;
21
22
    /** @var string */
23
    protected $environment;
24
25
    /** @var string */
26
    protected $certificate;
27
28
    /** @var string|null */
29
    protected $passPhrase;
30
31
    /** @var \ZendService\Apple\Apns\Client\Message */
32
    protected $client;
33
34
    /** @var \ZendService\Apple\Apns\Client\Feedback */
35
    protected $feedbackClient;
36
37
    /** @var \Illuminate\Events\Dispatcher */
38
    protected $events;
39
40
    /**
41
     * @param \ZendService\Apple\Apns\Client\Message $client
42
     * @param \ZendService\Apple\Apns\Client\Feedback $feedbackClient
43
     * @param \Illuminate\Events\Dispatcher $events
44
     * @param string $environment
45
     * @param string $certificate
46
     * @param string|null $passPhrase
47
     */
48 2
    public function __construct(
49
        Client $client,
50
        FeedbackClient $feedbackClient,
51
        Dispatcher $events,
52
        $environment,
53
        $certificate,
54
        $passPhrase = null
55
    ) {
56 2
        $this->client = $client;
57 2
        $this->feedbackClient = $feedbackClient;
58 2
        $this->events = $events;
59 2
        $this->environment = $environment;
60 2
        $this->certificate = $certificate;
61 2
        $this->passPhrase = $passPhrase;
62 2
    }
63
64
    /**
65
     * Send the notification to Apple Push Notification Service.
66
     *
67
     * @param mixed $notifiable
68
     * @param \Illuminate\Notifications\Notification $notification
69
     *
70
     * @throws \NotificationChannels\Apn\Exceptions\SendingFailed
71
     */
72 1
    public function send($notifiable, Notification $notification)
73
    {
74 1
        $tokens = (array) $notifiable->routeNotificationFor('apn');
75 1
        if (! $tokens) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tokens 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...
76
            return;
77
        }
78
79 1
        $message = $notification->toApn($notifiable);
80 1
        if (! $message) {
81
            return;
82
        }
83
84 1
        if (! $this->openConnection()) {
85
            return;
86
        }
87
88 1
        foreach ($tokens as $token) {
89
            try {
90 1
                $alert = new Alert();
91 1
                $alert->setTitle($message->title);
92 1
                $alert->setBody($message->body);
93
94 1
                $packet = new Packet();
95 1
                $packet->setToken($token);
96 1
                $packet->setBadge($message->badge);
97 1
                $packet->setSound($message->sound);
98 1
                $packet->setCategory($message->category);
99 1
                $packet->setAlert($alert);
100 1
                $packet->setCustom($message->custom);
101
102 1
                $response = $this->client->send($packet);
103
104 1
                if ($response->getCode() !== Response::RESULT_OK) {
105 1
                    $this->events->fire(
106 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...
107 1
                            'token' => $token,
108 1
                            'error' => $response->getCode(),
109 1
                        ])
110 1
                    );
111 1
                }
112 1
            } catch (Exception $e) {
113
                throw SendingFailed::create($e);
114
            }
115 1
        }
116
117 1
        $this->closeConnection();
118 1
    }
119
120
    /**
121
     * Get feedback from the Apple Feedback Service about failed deliveries
122
     *
123
     * @return array|ApnFeedback[]
124
     * @throws Exceptions\ConnectionFailed
125
     */
126 1
    public function getFeedback()
127
    {
128 1
        $client = $this->feedbackClient;
129
130
        try {
131 1
            $client->open($this->environment, $this->certificate, $this->passPhrase);
132 1
        } catch (Exception $exception) {
133
            throw Exceptions\ConnectionFailed::create($exception);
134
        }
135
136 1
        $feedback = [];
137
138
        /** @var FeedbackResponse $response */
139 1
        foreach ($client->feedback() as $response) {
140 1
            $feedback[] = new ApnFeedback($response->getToken(), $response->getTime());
141 1
        }
142
143 1
        $client->close();
144
145 1
        return $feedback;
146
    }
147
148
    /**
149
     * Open the connection.
150
     *
151
     * @return bool
152
     *
153
     * @throws \NotificationChannels\Apn\Exceptions\ConnectionFailed
154
     */
155 1
    private function openConnection()
156
    {
157
        try {
158 1
            $this->client->open($this->environment, $this->certificate, $this->passPhrase);
159
160 1
            return true;
161
        } catch (Exception $exception) {
162
            throw Exceptions\ConnectionFailed::create($exception);
163
        }
164
    }
165
166
    /**
167
     * Close the connection.
168
     */
169 1
    private function closeConnection()
170
    {
171 1
        $this->client->close();
172 1
    }
173
}
174