Completed
Branch master (69595d)
by Freek
10:42 queued 08:57
created

GcmChannel::getPacket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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