GcmChannel   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 19.05%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 110
ccs 8
cts 42
cp 0.1905
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 24 5
A getPacket() 0 18 1
A handleFailedNotifications() 0 17 3
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 5.9.

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
            ] + $message->notification);
93
94
        return $packet;
95
    }
96
97
    /**
98
     * Handle a failed notification.
99
     *
100
     * @param mixed $notifiable
101
     * @param \Illuminate\Notifications\Notification $notification
102
     * @param $response
103
     */
104
    protected function handleFailedNotifications($notifiable, Notification $notification, $response)
105
    {
106
        $results = $response->getResults();
107
108
        foreach ($results as $token => $result) {
109
            if (! isset($result['error'])) {
110
                continue;
111
            }
112
113
            $this->events->dispatch(
114
                new NotificationFailed($notifiable, $notification, get_class($this), [
115
                    'token' => $token,
116
                    'error' => $result['error'],
117
                ])
118
            );
119
        }
120
    }
121
}
122