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 ( 395778...35ff7e )
by Dwight
8s
created

ApnChannel   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 88.57%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 9
c 5
b 0
f 0
lcom 1
cbo 6
dl 0
loc 117
ccs 31
cts 35
cp 0.8857
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A send() 0 14 3
A sendNotifications() 0 15 2
A sendNotification() 0 17 3
1
<?php
2
3
namespace NotificationChannels\Apn;
4
5
use Exception;
6
use Illuminate\Events\Dispatcher;
7
use Illuminate\Notifications\Notification;
8
use ZendService\Apple\Apns\Client\Message as Client;
9
use NotificationChannels\Apn\Exceptions\SendingFailed;
10
use Illuminate\Notifications\Events\NotificationFailed;
11
use ZendService\Apple\Apns\Response\Message as Response;
12
13
class ApnChannel
14
{
15
    use InteractsWithConnection;
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
    /** @var \NotificationChannels\Apn\ApnCredentials */
38
    protected $credentials;
39
40
    /**
41
     * Create a new instance of the APN channel.
42
     *
43
     * @param  \ZendService\Apple\Apns\Client\Message  $client
44
     * @param  \Illuminate\Events\Dispatcher  $events
45
     * @param  \NotificationChannels\Apn\ApnCredentials  $credentials
46
     */
47 2
    public function __construct(
48
        Client $client,
49
        Dispatcher $events,
50
        ApnAdapter $adapter,
51
        ApnCredentials $credentials
52
    ) {
53 2
        $this->client = $client;
54 2
        $this->events = $events;
55 2
        $this->adapter = $adapter;
0 ignored issues
show
Bug introduced by
The property adapter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56 2
        $this->credentials = $credentials;
57 2
    }
58
59
    /**
60
     * Send the notification to Apple Push Notification Service.
61
     *
62
     * @param mixed $notifiable
63
     * @param \Illuminate\Notifications\Notification $notification
64
     */
65 2
    public function send($notifiable, Notification $notification)
66
    {
67 2
        $tokens = (array) $notifiable->routeNotificationFor('apn');
68 2
        if (empty($tokens)) {
69
            return;
70
        }
71
72 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...
73 2
        if (! $message) {
74
            return;
75
        }
76
77 2
        $this->sendNotifications($notifiable, $notification, $tokens, $message);
78 2
    }
79
80
    /**
81
     * Send the notification to each of the provided tokens.
82
     *
83
     * @param mixed $notifiable
84
     * @param \Illuminate\Notifications\Notification $notification
85
     */
86 2
    protected function sendNotifications($notifiable, $notification, $tokens, $message)
87
    {
88 2
        foreach ($tokens as $token) {
89 2
            $this->openConnection();
90
91 2
            $this->sendNotification(
92 2
                $notifiable,
93 2
                $notification,
94 2
                $this->adapter->adapt($message, $token),
95 2
                $token
96
            );
97
98 2
            $this->closeConnection();
99
        }
100 2
    }
101
102
    /**
103
     * Sent the notification to the given token.
104
     *
105
     * @param mixed $notifiable
106
     * @param \Illuminate\Notifications\Notification $notification
107
     * @param \ZendService\Apple\Apns\Message  $message
108
     * @param  string  $token
109
     * @return void
110
     * @throws \NotificationChannels\Apn\Exceptions\SendingFailed
111
     */
112 2
    protected function sendNotification($notifiable, $notification, $message, $token)
113
    {
114
        try {
115 2
            $response = $this->client->send($message);
116
117 2
            if ($response->getCode() !== Response::RESULT_OK) {
118 1
                $this->events->fire(
119 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...
120 1
                        'token' => $token,
121 2
                        'error' => $response->getCode(),
122
                    ])
123
                );
124
            }
125
        } catch (Exception $e) {
126
            throw SendingFailed::create($e);
127
        }
128 2
    }
129
}
130