Completed
Pull Request — master (#35)
by
unknown
12:18
created

GcmChannel::handleFailedNotifications()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.7
c 0
b 0
f 0
cc 3
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
    /**
15
     * The GCM client instance.
16
     *
17
     * @var \ZendService\Google\Gcm\Client
18
     */
19
    protected $client;
20
21
    /**
22
     * The event dispatcher instance.
23
     *
24
     * @var \Illuminate\Events\Dispatcher
25
     */
26
    protected $events;
27
28
    /**
29
     * Create a new channel instance.
30
     *
31
     * @param \ZendService\Google\Gcm\Client $client
32
     * @param \Illuminate\Events\Dispatcher $events
33
     */
34 1
    public function __construct(Client $client, Dispatcher $events)
35
    {
36 1
        $this->client = $client;
37 1
        $this->events = $events;
38 1
    }
39
40
    /**
41
     * Send the notification to Google Cloud Messaging.
42
     *
43
     * @param mixed $notifiable
44
     * @param Notification $notification
45
     * @return void
46
     * @throws Exceptions\SendingFailed
47
     */
48 1
    public function send($notifiable, Notification $notification)
49
    {
50 1
        $tokens = (array) $notifiable->routeNotificationFor('gcm', $notification);
51 1
        if (empty($tokens)) {
52 1
            return;
53
        }
54
55
        $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...
56
        if (! $message) {
57
            return;
58
        }
59
60
        $packet = $this->getPacket($tokens, $message);
61
62
        try {
63
            $response = $this->client->send($packet);
64
        } catch (Exception $exception) {
65
            throw SendingFailed::create($exception);
66
        }
67
68
        if (! $response->getFailureCount() == 0) {
69
            $this->handleFailedNotifications($notifiable, $notification, $response);
70
        }
71
    }
72
73
    /**
74
     * @param $tokens
75
     * @param $message
76
     * @return \NotificationChannels\Gcm\Packet
77
     */
78
    protected function getPacket($tokens, $message)
79
    {
80
        $packet = new Packet();
81
82
        $packet->setRegistrationIds($tokens);
83
        $packet->setCollapseKey(str_slug($message->title));
0 ignored issues
show
Deprecated Code introduced by
The function str_slug() has been deprecated with message: Str::slug() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
84
        $packet->setData([
85
                'title' => $message->title,
86
                'message' => $message->message,
87
            ] + $message->data);
88
        $packet->setNotification([
89
                'title' => $message->title,
90
                'body' => $message->message,
91
                'sound' => $message->sound,
92
                'icon' => $message->icon,
93
            ] + $message->notification);
94
95
        return $packet;
96
    }
97
98
    /**
99
     * Handle a failed notification.
100
     *
101
     * @param mixed $notifiable
102
     * @param \Illuminate\Notifications\Notification $notification
103
     * @param $response
104
     */
105
    protected function handleFailedNotifications($notifiable, Notification $notification, $response)
106
    {
107
        $results = $response->getResults();
108
109
        foreach ($results as $token => $result) {
110
            if (! isset($result['error'])) {
111
                continue;
112
            }
113
114
            $this->events->dispatch(
115
                new NotificationFailed($notifiable, $notification, get_class($this), [
116
                    'token' => $token,
117
                    'error' => $result['error'],
118
                ])
119
            );
120
        }
121
    }
122
}
123