From ffd690f2c8262da61f52161a8dddf7eab8f11b25 Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Sun, 24 Dec 2023 21:26:06 +1100 Subject: [PATCH] Return collection of responses from send --- src/FcmChannel.php | 30 ++++++++---------------------- tests/FcmChannelTest.php | 8 +++++--- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/FcmChannel.php b/src/FcmChannel.php index 076f35d..958f4d1 100644 --- a/src/FcmChannel.php +++ b/src/FcmChannel.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Notifications\Events\NotificationFailed; use Illuminate\Notifications\Notification; +use Illuminate\Support\Collection; use Illuminate\Support\Arr; use Kreait\Firebase\Contract\Messaging; use Kreait\Firebase\Messaging\MulticastSendReport; @@ -21,9 +22,6 @@ class FcmChannel /** * Create a new channel instance. - * - * @param Illuminate\Contracts\Events\Dispatcher $events - * @param Firebase\Contract\Messaging $client */ public function __construct(protected Dispatcher $events, protected Messaging $client) { @@ -32,22 +30,18 @@ public function __construct(protected Dispatcher $events, protected Messaging $c /** * Send the given notification. - * - * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification - * @return void */ - public function send($notifiable, Notification $notification): void + public function send(mixed $notifiable, Notification $notification): ?Collection { $tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification)); if (empty($tokens)) { - return; + return null; } $fcmMessage = $notification->toFcm($notifiable); - collect($tokens) + return collect($tokens) ->chunk(self::TOKENS_PER_REQUEST) ->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all())) ->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report)); @@ -55,28 +49,20 @@ public function send($notifiable, Notification $notification): void /** * Handle the report for the notification and dispatch any failed notifications. - * - * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification - * @param Kreait\Firebase\Messaging\MulticastSendReport. $report - * @return void */ - protected function checkReportForFailures($notifiable, $notification, MulticastSendReport $report) + protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport { collect($report->getItems()) ->filter(fn (SendReport $report) => $report->isFailure()) ->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report)); + + return $report; } /** * Dispatch failed event. - * - * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification - * @param \Kreait\Firebase\Messaging\SendReport $report - * @return void */ - protected function dispatchFailedNotification($notifiable, Notification $notification, SendReport $report): void + protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void { $this->events->dispatch(new NotificationFailed($notifiable, $notification, self::class, [ 'report' => $report, diff --git a/tests/FcmChannelTest.php b/tests/FcmChannelTest.php index f92b4d4..132262e 100644 --- a/tests/FcmChannelTest.php +++ b/tests/FcmChannelTest.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; +use Illuminate\Support\Collection; use Kreait\Firebase\Contract\Messaging; use Kreait\Firebase\Messaging\MessageTarget; use Kreait\Firebase\Messaging\MulticastSendReport; @@ -48,7 +49,8 @@ public function test_it_can_send_notifications() $result = $channel->send(new DummyNotifiable, new DummyNotification); - $this->assertNull($result); + $this->assertInstanceOf(Collection::class, $result); + $this->assertInstanceOf(MulticastSendReport::class, $result->first()); } public function test_it_can_send_notifications_with_custom_client() @@ -70,7 +72,7 @@ public function test_it_can_send_notifications_with_custom_client() $result = $channel->send(new DummyNotifiable, new DummyNotification($customFirebase)); - $this->assertNull($result); + $this->assertInstanceOf(Collection::class, $result); } public function test_it_can_dispatch_events() @@ -89,7 +91,7 @@ public function test_it_can_dispatch_events() $result = $channel->send(new DummyNotifiable, new DummyNotification); - $this->assertNull($result); + $this->assertInstanceOf(Collection::class, $result); } protected function target()