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 (#35)
by Dwight
03:02 queued 52s
created

ApnChannel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 91.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 89
ccs 34
cts 37
cp 0.9189
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B send() 0 46 6
1
<?php
2
3
namespace NotificationChannels\Apn;
4
5
use Illuminate\Events\Dispatcher;
6
use ZendService\Apple\Apns\Message\Alert;
7
use Illuminate\Notifications\Notification;
8
use ZendService\Apple\Apns\Message as Packet;
9
use ZendService\Apple\Apns\Client\Message as Client;
10
use NotificationChannels\Apn\Exceptions\SendingFailed;
11
use Illuminate\Notifications\Events\NotificationFailed;
12
use ZendService\Apple\Apns\Response\Message as Response;
13
14
class ApnChannel
15
{
16
    use InteractsWithConnection;
17
18
    const SANDBOX = 0;
19
    const PRODUCTION = 1;
20
21
    /** @var \ZendService\Apple\Apns\Client\Message */
22
    protected $client;
23
24
    /** @var \Illuminate\Events\Dispatcher */
25
    protected $events;
26
27
    /**
28
     * @param \ZendService\Apple\Apns\Client\Message $client
29
     * @param \Illuminate\Events\Dispatcher $events
30
     * @param string $environment
31
     * @param string $certificate
32
     * @param string|null $passPhrase
33
     */
34 2
    public function __construct(
35
        Client $client,
36
        Dispatcher $events,
37
        $environment,
38
        $certificate,
39
        $passPhrase = null
40
    ) {
41 2
        $this->client = $client;
42 2
        $this->events = $events;
43 2
        $this->environment = $environment;
0 ignored issues
show
Documentation Bug introduced by
The property $environment was declared of type integer, but $environment is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
44 2
        $this->certificate = $certificate;
45 2
        $this->passPhrase = $passPhrase;
46 2
    }
47
48
    /**
49
     * Send the notification to Apple Push Notification Service.
50
     *
51
     * @param mixed $notifiable
52
     * @param \Illuminate\Notifications\Notification $notification
53
     *
54
     * @throws \NotificationChannels\Apn\Exceptions\SendingFailed
55
     */
56 2
    public function send($notifiable, Notification $notification)
57
    {
58 2
        $tokens = (array) $notifiable->routeNotificationFor('apn');
59 2
        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...
60
            return;
61
        }
62
63 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...
64 2
        if (! $message) {
65
            return;
66
        }
67
68 2
        $this->openConnection();
69
70 2
        foreach ($tokens as $token) {
71
            try {
72 2
                $alert = new Alert();
73 2
                $alert->setTitle($message->title);
74 2
                $alert->setBody($message->body);
75
76 2
                $packet = new Packet();
77 2
                $packet->setToken($token);
78 2
                $packet->setBadge($message->badge);
79 2
                $packet->setSound($message->sound);
80 2
                $packet->setCategory($message->category);
81 2
                $packet->setContentAvailable($message->contentAvailable);
82 2
                $packet->setAlert($alert);
83 2
                $packet->setCustom($message->custom);
84
85 2
                $response = $this->client->send($packet);
86
87 2
                if ($response->getCode() !== Response::RESULT_OK) {
88 1
                    $this->events->fire(
89 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...
90 1
                            'token' => $token,
91 2
                            'error' => $response->getCode(),
92
                        ])
93
                    );
94
                }
95
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class NotificationChannels\Apn\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
96 2
                throw SendingFailed::create($e);
97
            }
98
        }
99
100 2
        $this->closeConnection();
101 2
    }
102
}
103