Completed
Pull Request — master (#13)
by Luis
03:32 queued 16s
created

GcmChannel::handleFailedNotifications()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 3
crap 12
1
<?php
2
3
namespace NotificationChannels\Gcm;
4
5
use Exception;
6
use Illuminate\Events\Dispatcher;
7
use ZendService\Google\Gcm\Client;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Gcm\Exceptions\SendingFailed;
10
use Illuminate\Notifications\Events\NotificationFailed;
11
12
class GcmChannel
13
{
14
    /** @var Client */
15
    protected $client;
16
17
    /** @var Dispatcher */
18
    protected $events;
19
20
    /**
21
     * @param Client $client
22
     * @param Dispatcher $events
23
     */
24 1
    public function __construct(Client $client, Dispatcher $events)
25
    {
26 1
        $this->client = $client;
27 1
        $this->events = $events;
28 1
    }
29
30
    /**
31
     * Send the notification to Google Cloud Messaging.
32
     *
33
     * @param mixed $notifiable
34
     * @param Notification $notification
35
     * @return void
36
     *
37
     * @throws Exceptions\SendingFailed
38
     */
39 1
    public function send($notifiable, Notification $notification)
40
    {
41 1
        $tokens = (array) $notifiable->routeNotificationFor('gcm');
42 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...
43 1
            return;
44
        }
45
46
        $message = $notification->toGcm($notifiable);
0 ignored issues
show
Bug introduced by
The method toGcm() 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...
47
        if (! $message) {
48
            return;
49
        }
50
51
        $packet = $this->getPacket($tokens, $message);
52
53
        try {
54
            $response = $this->client->send($packet);
55
        } catch (Exception $exception) {
56
            throw SendingFailed::create($exception);
57
        }
58
59
        if (! $response->getFailureCount() == 0) {
60
            $this->handleFailedNotifications($notifiable, $notification, $response);
61
        }
62
    }
63
64
    /**
65
     * @param $tokens
66
     * @param $message
67
     *
68
     * @return \NotificationChannels\Gcm\Packet
69
     */
70
    protected function getPacket($tokens, $message)
71
    {
72
        $packet = new Packet();
73
74
        $packet->setRegistrationIds($tokens);
75
        $packet->setCollapseKey(str_slug($message->title));
76
77
        if ($message->isAndroid()) {
78
            $packet->setData([
79
                'title' => $message->title,
80
                'message' => $message->message,
81
            ] + $message->data);
82
        }
83
84
        if ($message->isIOS()) {
85
            $packet->setNotification([
86
                'title' => $message->title,
87
                'body' => $message->message,
88
            ] + $message->data);
89
        }
90
91
        return $packet;
92
    }
93
94
    /**
95
     * @param $notifiable
96
     * @param \Illuminate\Notifications\Notification $notification
97
     * @param $response
98
     */
99
    protected function handleFailedNotifications($notifiable, Notification $notification, $response)
100
    {
101
        $results = $response->getResults();
102
103
        foreach ($results as $token => $result) {
104
            if (! isset($result['error'])) {
105
                continue;
106
            }
107
108
            $this->events->fire(
109
                new NotificationFailed($notifiable, $notification, $this, [
0 ignored issues
show
Documentation introduced by
$this is of type this<NotificationChannels\Gcm\GcmChannel>, 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...
110
                    'token' => $token,
111
                    'error' => $result['error'],
112
                ])
113
            );
114
        }
115
    }
116
}
117