Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Dispatch additional events" #213

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions src/FcmChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Events\NotificationSending;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -37,40 +35,37 @@ public function send(mixed $notifiable, Notification $notification): ?Collection
{
$tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification));

if (empty($tokens)) {
return null;
}

$fcmMessage = $notification->toFcm($notifiable);

return Collection::make($tokens)
->chunk(self::TOKENS_PER_REQUEST)
->map(fn ($tokens) => $this->sendNotifications($notifiable, $notification, $tokens))
->map(fn (MulticastSendReport $report) => $this->dispatchEvents($notifiable, $notification, $report));
->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()))
->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report));
}

/**
* Send the notification with the provided tokens.
* Handle the report for the notification and dispatch any failed notifications.
*/
protected function sendNotifications(mixed $notifiable, Notification $notification, Collection $tokens): MulticastSendReport
protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
{
$fcmMessage = $notification->toFcm($notifiable);

$this->events->dispatch(
new NotificationSending($notifiable, $notification, self::class)
);
Collection::make($report->getItems())
->filter(fn (SendReport $report) => $report->isFailure())
->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report));

return ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all());
return $report;
}

/**
* Handle the report for the notification and dispatch any failed notifications.
* Dispatch failed event.
*/
protected function dispatchEvents(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void
{
Collection::make($report->getItems())
->each(function (SendReport $report) use ($notifiable, $notification) {
$event = $report->isSuccess()
? new NotificationSent($notifiable, $notification, self::class, compact('report'))
: new NotificationFailed($notifiable, $notification, self::class, compact('report'));

$this->events->dispatch($event);
});

return $report;
$this->events->dispatch(new NotificationFailed($notifiable, $notification, self::class, [
'report' => $report,
]));
}
}
30 changes: 3 additions & 27 deletions tests/FcmChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Exception;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Events\NotificationSending;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -39,14 +36,7 @@ public function test_it_can_be_instantiated()
public function test_it_can_send_notifications()
{
$events = Mockery::mock(Dispatcher::class);
$events->shouldReceive('dispatch')
->once()
->with(Mockery::type(NotificationSending::class));
$events->shouldReceive('dispatch')
->once()
->with(Mockery::type(NotificationSent::class));
$events->shouldNotReceive('dispatch')
->with(Mockery::type(NotificationFailed::class));
$events->shouldNotReceive('dispatch');

$firebase = Mockery::mock(Messaging::class);
$firebase->shouldReceive('sendMulticast')
Expand All @@ -66,14 +56,7 @@ public function test_it_can_send_notifications()
public function test_it_can_send_notifications_with_custom_client()
{
$events = Mockery::mock(Dispatcher::class);
$events->shouldReceive('dispatch')
->once()
->with(Mockery::type(NotificationSending::class));
$events->shouldReceive('dispatch')
->once()
->with(Mockery::type(NotificationSent::class));
$events->shouldNotReceive('dispatch')
->with(Mockery::type(NotificationFailed::class));
$events->shouldNotReceive('dispatch');

$firebase = Mockery::mock(Messaging::class);
$events->shouldNotReceive('sendMulticast');
Expand All @@ -95,14 +78,7 @@ public function test_it_can_send_notifications_with_custom_client()
public function test_it_can_dispatch_events()
{
$events = Mockery::mock(Dispatcher::class);
$events->shouldReceive('dispatch')
->once()
->with(Mockery::type(NotificationSending::class));
$events->shouldNotReceive('dispatch')
->with(Mockery::type(NotificationSent::class));
$events->shouldReceive('dispatch')
->once()
->with(Mockery::type(NotificationFailed::class));
$events->shouldReceive('dispatch')->once();

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