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 ( a35b5d...43dfbc )
by Barry vd.
02:28
created

src/ApnChannel.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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