1 | <?php |
||
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) |
|
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) { |
|
|
|||
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) |
||
84 | |||
85 | /** |
||
86 | * @param $notifiable |
||
87 | * @param \Illuminate\Notifications\Notification $notification |
||
88 | * @param $response |
||
89 | */ |
||
90 | protected function handleFailedNotifications($notifiable, Notification $notification, $response) |
||
107 | } |
||
108 |
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.