<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
owowagency / laravel-nofitication-bundler example snippets
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Owowagency\NotificationBundler\BundlesNotifications;
use Owowagency\NotificationBundler\ShouldBundleNotifications;
class BundledMailNotification extends Notification implements ShouldBundleNotifications, ShouldQueue
{
use BundlesNotifications, Queueable;
public function __construct(public string $name)
{
//
}
/**
* The channels the notification should be sent on.
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
* This replaces the original `toMail` method and adds the $notifications
* collection as the last parameter.
*/
public function toMailBundle(object $notifiable, Collection $notifications)
{
$message = (new MailMessage)
->subject('Bundle');
foreach ($notifications as $notification) {
$message->line("$notification->name was bundled.");
}
return $message;
}
/**
* Returns the identifier for the bundle.
* This is used to determine which notifications should be bundled together.
* This also means that different notifications can be bundled together.
*/
public function bundleIdentifier(object $notifiable): string
{
return "user_$notifiable->id";
}
}
public function toDatabase(object $notifiable): array
{
$notifications = $this->getBundle();
return ['names' => $notifications->pluck('name')->toArray()];
}
class CustomMiddlewareNotification extends Notification implements ShouldBundleNotifications, ShouldQueue
{
use BundlesNotifications {
middleware as bundledMiddleware;
}
// ...
public function middleware(object $notifiable): array
{
return [
...$this->bundledMiddleware($notifiable), // First apply the bundled middleware.
StopExecution::class, // Then apply your own middleware.
];
}
}
public function bundleDelay(object $notifiable): int|\DateTimeInterface
{
return 60;
}
public function withDelay(object $notifiable): array
{
return [
'mail' => 30,
'sms' => 60,
];
}
public function bundleChannels(): array
{
return ['mail'];
}