Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.33% covered (danger)
8.33%
1 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SendNotificationJob
8.33% covered (danger)
8.33%
1 / 12
50.00% covered (danger)
50.00%
1 / 2
9.93
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Jobs;
4
5use App\Enums\NotificationStatus;
6use App\Models\NotificationLog;
7use App\Services\Notification\NotificationManager;
8use Illuminate\Bus\Queueable;
9use Illuminate\Contracts\Queue\ShouldQueue;
10use Illuminate\Foundation\Bus\Dispatchable;
11use Illuminate\Queue\InteractsWithQueue;
12use Illuminate\Queue\SerializesModels;
13
14/**
15 * One channel, one recipient, one send — queued so a slow SMS/email API
16 * never holds up the checkout/admin request that triggered it. Every
17 * attempt is logged to notification_logs regardless of outcome, for
18 * Settings → Notification Settings' troubleshooting log.
19 */
20class SendNotificationJob implements ShouldQueue
21{
22    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
23
24    public function __construct(
25        public readonly string $event,
26        public readonly string $channel,
27        public readonly string $recipient,
28        public readonly string $subject,
29        public readonly string $body,
30    ) {}
31
32    public function handle(NotificationManager $manager): void
33    {
34        $log = NotificationLog::create([
35            'event' => $this->event,
36            'channel' => $this->channel,
37            'recipient' => $this->recipient,
38            'status' => NotificationStatus::Pending->value,
39        ]);
40
41        $result = $manager->send($this->channel, $this->recipient, $this->subject, $this->body);
42
43        $log->update([
44            'status' => $result['ok'] ? NotificationStatus::Sent->value : NotificationStatus::Failed->value,
45            'response' => $result['response'],
46        ]);
47    }
48}