Skip to content

Commit 5bd56e3

Browse files
authored
Revert "Dispatch additional events"
1 parent 2685d4f commit 5bd56e3

File tree

2 files changed

+22
-51
lines changed

2 files changed

+22
-51
lines changed

src/FcmChannel.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Notifications\Events\NotificationFailed;
7-
use Illuminate\Notifications\Events\NotificationSending;
8-
use Illuminate\Notifications\Events\NotificationSent;
97
use Illuminate\Notifications\Notification;
108
use Illuminate\Support\Arr;
119
use Illuminate\Support\Collection;
@@ -37,40 +35,37 @@ public function send(mixed $notifiable, Notification $notification): ?Collection
3735
{
3836
$tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification));
3937

38+
if (empty($tokens)) {
39+
return null;
40+
}
41+
42+
$fcmMessage = $notification->toFcm($notifiable);
43+
4044
return Collection::make($tokens)
4145
->chunk(self::TOKENS_PER_REQUEST)
42-
->map(fn ($tokens) => $this->sendNotifications($notifiable, $notification, $tokens))
43-
->map(fn (MulticastSendReport $report) => $this->dispatchEvents($notifiable, $notification, $report));
46+
->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()))
47+
->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report));
4448
}
4549

4650
/**
47-
* Send the notification with the provided tokens.
51+
* Handle the report for the notification and dispatch any failed notifications.
4852
*/
49-
protected function sendNotifications(mixed $notifiable, Notification $notification, Collection $tokens): MulticastSendReport
53+
protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
5054
{
51-
$fcmMessage = $notification->toFcm($notifiable);
52-
53-
$this->events->dispatch(
54-
new NotificationSending($notifiable, $notification, self::class)
55-
);
55+
Collection::make($report->getItems())
56+
->filter(fn (SendReport $report) => $report->isFailure())
57+
->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report));
5658

57-
return ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all());
59+
return $report;
5860
}
5961

6062
/**
61-
* Handle the report for the notification and dispatch any failed notifications.
63+
* Dispatch failed event.
6264
*/
63-
protected function dispatchEvents(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
65+
protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void
6466
{
65-
Collection::make($report->getItems())
66-
->each(function (SendReport $report) use ($notifiable, $notification) {
67-
$event = $report->isSuccess()
68-
? new NotificationSent($notifiable, $notification, self::class, compact('report'))
69-
: new NotificationFailed($notifiable, $notification, self::class, compact('report'));
70-
71-
$this->events->dispatch($event);
72-
});
73-
74-
return $report;
67+
$this->events->dispatch(new NotificationFailed($notifiable, $notification, self::class, [
68+
'report' => $report,
69+
]));
7570
}
7671
}

tests/FcmChannelTest.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
use Exception;
66
use Illuminate\Contracts\Events\Dispatcher;
7-
use Illuminate\Notifications\Events\NotificationFailed;
8-
use Illuminate\Notifications\Events\NotificationSending;
9-
use Illuminate\Notifications\Events\NotificationSent;
107
use Illuminate\Notifications\Notifiable;
118
use Illuminate\Notifications\Notification;
129
use Illuminate\Support\Collection;
@@ -39,14 +36,7 @@ public function test_it_can_be_instantiated()
3936
public function test_it_can_send_notifications()
4037
{
4138
$events = Mockery::mock(Dispatcher::class);
42-
$events->shouldReceive('dispatch')
43-
->once()
44-
->with(Mockery::type(NotificationSending::class));
45-
$events->shouldReceive('dispatch')
46-
->once()
47-
->with(Mockery::type(NotificationSent::class));
48-
$events->shouldNotReceive('dispatch')
49-
->with(Mockery::type(NotificationFailed::class));
39+
$events->shouldNotReceive('dispatch');
5040

5141
$firebase = Mockery::mock(Messaging::class);
5242
$firebase->shouldReceive('sendMulticast')
@@ -66,14 +56,7 @@ public function test_it_can_send_notifications()
6656
public function test_it_can_send_notifications_with_custom_client()
6757
{
6858
$events = Mockery::mock(Dispatcher::class);
69-
$events->shouldReceive('dispatch')
70-
->once()
71-
->with(Mockery::type(NotificationSending::class));
72-
$events->shouldReceive('dispatch')
73-
->once()
74-
->with(Mockery::type(NotificationSent::class));
75-
$events->shouldNotReceive('dispatch')
76-
->with(Mockery::type(NotificationFailed::class));
59+
$events->shouldNotReceive('dispatch');
7760

7861
$firebase = Mockery::mock(Messaging::class);
7962
$events->shouldNotReceive('sendMulticast');
@@ -95,14 +78,7 @@ public function test_it_can_send_notifications_with_custom_client()
9578
public function test_it_can_dispatch_events()
9679
{
9780
$events = Mockery::mock(Dispatcher::class);
98-
$events->shouldReceive('dispatch')
99-
->once()
100-
->with(Mockery::type(NotificationSending::class));
101-
$events->shouldNotReceive('dispatch')
102-
->with(Mockery::type(NotificationSent::class));
103-
$events->shouldReceive('dispatch')
104-
->once()
105-
->with(Mockery::type(NotificationFailed::class));
81+
$events->shouldReceive('dispatch')->once();
10682

10783
$firebase = Mockery::mock(Messaging::class);
10884
$firebase->shouldReceive('sendMulticast')

0 commit comments

Comments
 (0)