Completed
Push — master ( 395778...35ff7e )
by Dwight
8s
created

ApnChannel::sendNotifications()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 4
crap 2
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 Dwight Watson
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 Robin van Nunen
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 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...
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