Completed
Push — master ( 924db6...cc24e6 )
by Dwight
10:25 queued 05:55
created

GcmChannel::send()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 20
cp 0
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 2
crap 30
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
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
    public function __construct(Client $client, Dispatcher $events)
25
    {
26
        $this->client = $client;
27
        $this->events = $events;
28
    }
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
    public function send($notifiable, Notification $notification)
40
    {
41
        $tokens = (array) $notifiable->routeNotificationFor('gcm');
42
        if (empty($tokens)) {
43
            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
        $packet->setData([
77
                'title' => $message->title,
78
                'message' => $message->message,
79
            ] + $message->data);
80
        $packet->setNotification([
81
                'title' => $message->title,
82
                'body' => $message->message,
83
                'sound' => $message->sound,
84
            ] + $message->data);
85
86
        return $packet;
87
    }
88
89
    /**
90
     * @param $notifiable
91
     * @param \Illuminate\Notifications\Notification $notification
92
     * @param $response
93
     */
94
    protected function handleFailedNotifications($notifiable, Notification $notification, $response)
95
    {
96
        $results = $response->getResults();
97
98
        foreach ($results as $token => $result) {
99
            if (! isset($result['error'])) {
100
                continue;
101
            }
102
103
            $this->events->fire(
104
                new NotificationFailed($notifiable, $notification, get_class($this), [
105
                    'token' => $token,
106
                    'error' => $result['error'],
107
                ])
108
            );
109
        }
110
    }
111
}
112