Files
ismysiteup.online/app/Notifications/BrokenLinkAlert.php
T
2026-01-17 13:46:20 +02:00

86 lines
2.4 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 BrokenLinkAlert extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(
public Site $site,
public array $brokenLinks
) {
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
$preferences = $notifiable->notification_preferences ?? [];
$channels = $preferences['broken_links'] ?? ['mail'];
return $channels;
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$brokenLinkCount = count($this->brokenLinks);
$message = (new MailMessage)
->warning()
->subject("⚠️ Broken Links Detected: {$this->site->name}")
->line("We detected **{$brokenLinkCount}** broken link(s) on **{$this->site->name}**.")
->line("Site URL: {$this->site->url}")
->line('')
->line('**Broken Links:**');
// Add up to 10 broken links to the email
$displayLimit = min(10, $brokenLinkCount);
foreach (array_slice($this->brokenLinks, 0, $displayLimit) as $link) {
$message->line("• {$link['url']} (HTTP {$link['status_code']})");
}
if ($brokenLinkCount > $displayLimit) {
$remaining = $brokenLinkCount - $displayLimit;
$message->line("... and {$remaining} more");
}
$message->line('')
->action('View All Broken Links', url("/sites/{$this->site->id}/broken-links"))
->line('Please fix these broken links to improve user experience and SEO.');
return $message;
}
/**
* 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,
'broken_link_count' => count($this->brokenLinks),
'broken_links' => $this->brokenLinks,
];
}
}