Files
2026-01-17 13:46:20 +02:00

119 lines
3.9 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\Site;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
class SslExpiryAlert extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
public Site $site,
public string $issuer,
public Carbon $expiresAt,
public int $daysRemaining,
public bool $isExpired = false
) {
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
$preferences = $notifiable->notification_preferences ?? [];
$channels = $preferences['ssl'] ?? ['mail'];
return $channels;
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$message = (new MailMessage)
->subject($this->isExpired
? "🔴 SSL Certificate Expired: {$this->site->name}"
: "⚠️ SSL Certificate Expiring Soon: {$this->site->name}")
->line($this->isExpired
? "The SSL certificate for **{$this->site->name}** has **expired**."
: "The SSL certificate for **{$this->site->name}** is expiring soon.")
->line("URL: {$this->site->url}")
->line("Issuer: {$this->issuer}")
->line($this->isExpired
? "Expired on: {$this->expiresAt->format('Y-m-d')}"
: "Expires on: {$this->expiresAt->format('Y-m-d')} ({$this->daysRemaining} days remaining)")
->action('View Site Details', url("/sites/{$this->site->id}"));
if ($this->isExpired) {
$message->error();
$message->line('⚠️ Your site visitors may see security warnings. Please renew the certificate immediately.');
} else {
$message->warning();
$message->line('Please renew the certificate before it expires to avoid service disruption.');
}
return $message;
}
/**
* Get the Slack representation of the notification.
*/
public function toSlack(object $notifiable): SlackMessage
{
$color = $this->isExpired ? 'danger' : 'warning';
$emoji = $this->isExpired ? '🔴' : '⚠️';
return (new SlackMessage)
->from('Site Monitor')
->to(config('services.slack.channel'))
->content($this->isExpired
? "{$emoji} SSL Certificate Expired!"
: "{$emoji} SSL Certificate Expiring Soon!")
->attachment(function ($attachment) use ($color) {
$attachment
->color($color)
->title($this->site->name, url("/sites/{$this->site->id}"))
->fields([
'URL' => $this->site->url,
'Issuer' => $this->issuer,
'Expires' => $this->expiresAt->format('Y-m-d'),
'Status' => $this->isExpired
? 'EXPIRED'
: "{$this->daysRemaining} days remaining",
]);
});
}
/**
* 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,
'issuer' => $this->issuer,
'expires_at' => $this->expiresAt->toDateTimeString(),
'days_remaining' => $this->daysRemaining,
'is_expired' => $this->isExpired,
];
}
}