Files
SAE-BUT2-backend/app/Notifications/EventDeleted.php
T

59 lines
1.5 KiB
PHP

<?php
namespace App\Notifications;
use App\Mail\EventParticipationCancelledMail;
use App\Models\Event;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Support\Facades\Mail;
class EventDeleted extends Notification implements ShouldBroadcast
{
use Queueable;
public function __construct(public Event $event) {}
public function via($notifiable): array
{
$channels = [];
if ($notifiable->web_notifications) {
$channels[] = 'database';
$channels[] = 'broadcast';
}
if ($notifiable->email_notifications) {
$channels[] = 'mail';
}
return $channels;
}
public function toDatabase($notifiable): array
{
return [
'message' => "L'événement {$this->event->name} a été supprimé. Vous n'y participez donc plus.",
'event_id' => $this->event->id,
];
}
public function toBroadcast($notifiable): BroadcastMessage
{
return new BroadcastMessage($this->toDatabase($notifiable));
}
public function toMail($notifiable): void
{
Mail::to($notifiable->email)->send(new EventParticipationCancelledMail([
'name' => $notifiable->name,
'lastname' => $notifiable->lastname,
'eventName' => $this->event->name,
'start' => $this->event->start,
'end' => $this->event->end,
]));
}
}