Completed
Push — master ( d210a4...3d143c )
by Barry vd.
07:35 queued 05:30
created

ApnChannel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 117
ccs 42
cts 48
cp 0.875
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A openConnection() 0 10 2
A closeConnection() 0 4 1
C send() 0 46 7
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;
0 ignored issues
show
Bug introduced by Barry vd. Heuvel
The property passphrase does not seem to exist. Did you mean passPhrase?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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 Robin van Nunen
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, [
0 ignored issues
show
Documentation introduced by Robin van Nunen
$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...
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