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 (#64)
by Talha Zekeriya
03:13
created

ApnChannel::send()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.3244

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 8
cts 11
cp 0.7272
rs 9.6333
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4.3244
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
        if (method_exists($notification, 'apnOn')) {
68
            $tokens = (array) $notification->apnOn($notifiable);
69
        } else {
70 2
            $tokens = (array) $notifiable->routeNotificationFor('apn', $notification);
71
        }
72
73 2
        if (empty($tokens)) {
74
            return;
75
        }
76
77 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...
78 2
        if (! $message) {
79
            return;
80
        }
81
82 2
        $this->sendNotifications($notifiable, $notification, $tokens, $message);
83 2
    }
84
85
    /**
86
     * Send the notification to each of the provided tokens.
87
     *
88
     * @param mixed $notifiable
89
     * @param \Illuminate\Notifications\Notification $notification
90
     */
91 2
    protected function sendNotifications($notifiable, $notification, $tokens, $message)
92
    {
93 2
        foreach ($tokens as $token) {
94 2
            $this->openConnection($message->credentials);
95
96 2
            $this->sendNotification(
97 2
                $notifiable,
98 2
                $notification,
99 2
                $this->adapter->adapt($message, $token),
100 2
                $token
101
            );
102
103 2
            $this->closeConnection();
104
        }
105 2
    }
106
107
    /**
108
     * Sent the notification to the given token.
109
     *
110
     * @param mixed $notifiable
111
     * @param \Illuminate\Notifications\Notification $notification
112
     * @param \ZendService\Apple\Apns\Message  $message
113
     * @param  string  $token
114
     * @return void
115
     * @throws \NotificationChannels\Apn\Exceptions\SendingFailed
116
     */
117 2
    protected function sendNotification($notifiable, $notification, $message, $token)
118
    {
119
        try {
120 2
            $response = $this->client->send($message);
121
122 2
            if ($response->getCode() !== Response::RESULT_OK) {
123 1
                $this->events->dispatch(
124 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...
125 1
                        'token' => $token,
126 2
                        'error' => $response->getCode(),
127
                    ])
128
                );
129
            }
130
        } catch (Exception $e) {
131
            throw SendingFailed::create($e);
132
        }
133 2
    }
134
}
135