Files
trackbugs/app/Notifications/NewIssueNotification.php
2026-03-19 19:47:06 +02:00

62 lines
1.7 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\Issue;
use App\Models\Project;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewIssueNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(
public Project $project,
public Issue $issue,
) {}
public function via(object $notifiable): array
{
$pref = $notifiable->notificationPreferenceFor($this->project);
$channels = [];
if ($pref->database_enabled) {
$channels[] = 'database';
}
if ($pref->email_enabled) {
$channels[] = 'mail';
}
return $channels;
}
public function toMail(object $notifiable): MailMessage
{
$url = route('projects.issues.show', [$this->project, $this->issue]);
return (new MailMessage)
->subject("[{$this->project->name}] New issue: {$this->issue->title}")
->greeting("New issue in {$this->project->name}")
->line($this->issue->title)
->when($this->issue->culprit, fn ($mail) => $mail->line("in {$this->issue->culprit}"))
->action('View Issue', $url)
->line("Level: {$this->issue->level}");
}
public function toArray(object $notifiable): array
{
return [
'type' => 'new_issue',
'project_id' => $this->project->id,
'project_name' => $this->project->name,
'issue_id' => $this->issue->id,
'issue_title' => $this->issue->title,
'level' => $this->issue->level,
];
}
}