73 lines
2.1 KiB
PHP
73 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Notifications;
|
|
|
|
use App\Models\Site;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
use Illuminate\Notifications\Notification;
|
|
|
|
class UptimeAlert extends Notification implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
/**
|
|
* Create a new notification instance.
|
|
*/
|
|
public function __construct(
|
|
public Site $site,
|
|
public ?int $statusCode,
|
|
public ?int $responseTime
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Get the notification's delivery channels.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function via(object $notifiable): array
|
|
{
|
|
$preferences = $notifiable->notification_preferences ?? [];
|
|
$channels = $preferences['uptime'] ?? ['mail'];
|
|
|
|
return $channels;
|
|
}
|
|
|
|
/**
|
|
* Get the mail representation of the notification.
|
|
*/
|
|
public function toMail(object $notifiable): MailMessage
|
|
{
|
|
$status = $this->statusCode ? "returned HTTP {$this->statusCode}" : "is unreachable";
|
|
|
|
return (new MailMessage)
|
|
->error()
|
|
->subject("⚠️ Site Down Alert: {$this->site->name}")
|
|
->line("Your site **{$this->site->name}** is experiencing downtime.")
|
|
->line("URL: {$this->site->url}")
|
|
->line("Status: {$status}")
|
|
->line("Response Time: " . ($this->responseTime ? "{$this->responseTime}ms" : "N/A"))
|
|
->line("Detected at: " . now()->format('Y-m-d H:i:s'))
|
|
->action('View Site Details', url("/sites/{$this->site->id}"))
|
|
->line('Please investigate the issue as soon as possible.');
|
|
}
|
|
|
|
/**
|
|
* Get the array representation of the notification.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(object $notifiable): array
|
|
{
|
|
return [
|
|
'site_id' => $this->site->id,
|
|
'site_name' => $this->site->name,
|
|
'site_url' => $this->site->url,
|
|
'status_code' => $this->statusCode,
|
|
'response_time' => $this->responseTime,
|
|
];
|
|
}
|
|
}
|