Skip to content

Commit 03178bb

Browse files
authored
[Chore] Refactored mail notifications to their own listeners (alexjustesen#1269)
1 parent 88b4c86 commit 03178bb

11 files changed

+214
-121
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Listeners\Mail;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Mail\SpeedtestCompletedMail;
7+
use App\Settings\NotificationSettings;
8+
use Illuminate\Support\Facades\Log;
9+
use Illuminate\Support\Facades\Mail;
10+
11+
class SendSpeedtestCompletedNotification
12+
{
13+
/**
14+
* Handle the event.
15+
*/
16+
public function handle(SpeedtestCompleted $event): void
17+
{
18+
$notificationSettings = new NotificationSettings();
19+
20+
if (! $notificationSettings->mail_enabled) {
21+
return;
22+
}
23+
24+
if (! $notificationSettings->mail_on_speedtest_run) {
25+
return;
26+
}
27+
28+
if (! count($notificationSettings->mail_recipients)) {
29+
Log::warning('Mail recipients not found, check mail notification channel settings.');
30+
31+
return;
32+
}
33+
34+
foreach ($notificationSettings->mail_recipients as $recipient) {
35+
Mail::to($recipient)
36+
->send(new SpeedtestCompletedMail($event->result));
37+
}
38+
}
39+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace App\Listeners\Mail;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Mail\SpeedtestThresholdMail;
8+
use App\Settings\NotificationSettings;
9+
use App\Settings\ThresholdSettings;
10+
use Illuminate\Support\Facades\Log;
11+
use Illuminate\Support\Facades\Mail;
12+
13+
class SendSpeedtestThresholdNotification
14+
{
15+
/**
16+
* Handle the event.
17+
*/
18+
public function handle(SpeedtestCompleted $event): void
19+
{
20+
$notificationSettings = new NotificationSettings();
21+
22+
if (! $notificationSettings->mail_enabled) {
23+
return;
24+
}
25+
26+
if (! $notificationSettings->mail_on_threshold_failure) {
27+
return;
28+
}
29+
30+
if (! count($notificationSettings->mail_recipients) > 0) {
31+
Log::warning('Mail recipients not found, check mail notification channel settings.');
32+
33+
return;
34+
}
35+
36+
$thresholdSettings = new ThresholdSettings();
37+
38+
$failed = [];
39+
40+
if ($thresholdSettings->absolute_download > 0) {
41+
array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings));
42+
}
43+
44+
if ($thresholdSettings->absolute_upload > 0) {
45+
array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings));
46+
}
47+
48+
if ($thresholdSettings->absolute_ping > 0) {
49+
array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings));
50+
}
51+
52+
if (! count($failed)) {
53+
return;
54+
}
55+
56+
foreach ($notificationSettings->mail_recipients as $recipient) {
57+
Mail::to($recipient)
58+
->send(new SpeedtestThresholdMail($event->result, $failed));
59+
}
60+
}
61+
62+
/**
63+
* Build mail notification if absolute download threshold is breached.
64+
*/
65+
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
66+
{
67+
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) {
68+
return [];
69+
}
70+
71+
return [
72+
'name' => 'Download',
73+
'threshold' => $thresholdSettings->absolute_download.' Mbps',
74+
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
75+
];
76+
}
77+
78+
/**
79+
* Build webhook notification if absolute upload threshold is breached.
80+
*/
81+
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
82+
{
83+
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) {
84+
return [];
85+
}
86+
87+
return [
88+
'name' => 'Upload',
89+
'threshold' => $thresholdSettings->absolute_upload.' Mbps',
90+
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
91+
];
92+
}
93+
94+
/**
95+
* Build webhook notification if absolute ping threshold is breached.
96+
*/
97+
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
98+
{
99+
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) {
100+
return [];
101+
}
102+
103+
return [
104+
'name' => 'Ping',
105+
'threshold' => $thresholdSettings->absolute_ping.' ms',
106+
'value' => round($event->result->ping, 2).' ms',
107+
];
108+
}
109+
}

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace App\Listeners;
44

55
use App\Events\SpeedtestCompleted;
6-
use App\Mail\SpeedtestCompletedMail;
76
use App\Settings\GeneralSettings;
87
use App\Settings\NotificationSettings;
98
use App\Telegram\TelegramNotification;
10-
use Illuminate\Support\Facades\Mail;
119
use Spatie\WebhookServer\WebhookCall;
1210

1311
class SpeedtestCompletedListener
@@ -33,15 +31,6 @@ public function __construct()
3331
*/
3432
public function handle(SpeedtestCompleted $event): void
3533
{
36-
if ($this->notificationSettings->mail_enabled) {
37-
if ($this->notificationSettings->mail_on_speedtest_run && count($this->notificationSettings->mail_recipients)) {
38-
foreach ($this->notificationSettings->mail_recipients as $recipient) {
39-
Mail::to($recipient)
40-
->send(new SpeedtestCompletedMail($event->result));
41-
}
42-
}
43-
}
44-
4534
if ($this->notificationSettings->telegram_enabled) {
4635
if ($this->notificationSettings->telegram_on_speedtest_run && count($this->notificationSettings->telegram_recipients)) {
4736
foreach ($this->notificationSettings->telegram_recipients as $recipient) {

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace App\Listeners\Threshold;
44

55
use App\Events\SpeedtestCompleted;
6-
use App\Mail\Threshold\AbsoluteMail;
76
use App\Settings\GeneralSettings;
87
use App\Settings\NotificationSettings;
98
use App\Settings\ThresholdSettings;
109
use App\Telegram\TelegramNotification;
1110
use Illuminate\Contracts\Queue\ShouldQueue;
1211
use Illuminate\Support\Facades\Log;
13-
use Illuminate\Support\Facades\Mail;
1412
use Spatie\WebhookServer\WebhookCall;
1513

1614
class AbsoluteListener implements ShouldQueue
@@ -44,11 +42,6 @@ public function handle(SpeedtestCompleted $event): void
4442
return;
4543
}
4644

47-
// Mail notification channel
48-
if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) {
49-
$this->mailChannel($event);
50-
}
51-
5245
// Telegram notification channel
5346
if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) {
5447
$this->telegramChannel($event);
@@ -60,58 +53,6 @@ public function handle(SpeedtestCompleted $event): void
6053
}
6154
}
6255

63-
/**
64-
* Handle database notifications.
65-
*/
66-
protected function mailChannel(SpeedtestCompleted $event): void
67-
{
68-
$failedThresholds = [];
69-
70-
if (! count($this->notificationSettings->mail_recipients) > 0) {
71-
Log::info('Skipping sending mail notification, no recipients.');
72-
}
73-
74-
// Download threshold
75-
if ($this->thresholdSettings->absolute_download > 0) {
76-
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
77-
array_push($failedThresholds, [
78-
'name' => 'Download',
79-
'threshold' => $this->thresholdSettings->absolute_download.' Mbps',
80-
'value' => toBits(convertSize($event->result->download), 2).' Mbps',
81-
]);
82-
}
83-
}
84-
85-
// Upload threshold
86-
if ($this->thresholdSettings->absolute_upload > 0) {
87-
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
88-
array_push($failedThresholds, [
89-
'name' => 'Upload',
90-
'threshold' => $this->thresholdSettings->absolute_upload.' Mbps',
91-
'value' => toBits(convertSize($event->result->upload), 2).'Mbps',
92-
]);
93-
}
94-
}
95-
96-
// Ping threshold
97-
if ($this->thresholdSettings->absolute_ping > 0) {
98-
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
99-
array_push($failedThresholds, [
100-
'name' => 'Ping',
101-
'threshold' => $this->thresholdSettings->absolute_ping.' ms',
102-
'value' => round($event->result->ping, 2).' ms',
103-
]);
104-
}
105-
}
106-
107-
if (count($failedThresholds)) {
108-
foreach ($this->notificationSettings->mail_recipients as $recipient) {
109-
Mail::to($recipient)
110-
->send(new AbsoluteMail($event->result, $failedThresholds));
111-
}
112-
}
113-
}
114-
11556
/**
11657
* Handle telegram notifications.
11758
*/

app/Listeners/Webhook/SendSpeedtestThresholdNotification.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol
7979

8080
return [
8181
'name' => 'Download',
82-
'threshold' => $thresholdSettings->absolute_download,
82+
'threshold' => $thresholdSettings->absolute_download.' Mbps',
8383
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
8484
];
8585
}
@@ -95,7 +95,7 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS
9595

9696
return [
9797
'name' => 'Upload',
98-
'threshold' => $thresholdSettings->absolute_upload,
98+
'threshold' => $thresholdSettings->absolute_upload.' Mbps',
9999
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
100100
];
101101
}
@@ -111,8 +111,8 @@ protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSet
111111

112112
return [
113113
'name' => 'Ping',
114-
'threshold' => $thresholdSettings->absolute_ping,
115-
'value' => round($event->result->ping, 2),
114+
'threshold' => $thresholdSettings->absolute_ping.' ms',
115+
'value' => round($event->result->ping, 2).' ms',
116116
];
117117
}
118118
}

app/Mail/SpeedtestCompletedMail.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
namespace App\Mail;
44

5+
use App\Helpers\Number;
56
use App\Models\Result;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Mail\Mailable;
910
use Illuminate\Mail\Mailables\Content;
1011
use Illuminate\Mail\Mailables\Envelope;
1112
use Illuminate\Queue\SerializesModels;
13+
use Illuminate\Support\Str;
1214

1315
class SpeedtestCompletedMail extends Mailable implements ShouldQueue
1416
{
1517
use Queueable, SerializesModels;
1618

17-
public $result;
18-
1919
/**
2020
* Create a new message instance.
2121
*
2222
* @return void
2323
*/
24-
public function __construct(Result $result)
25-
{
26-
$this->result = $result;
24+
public function __construct(
25+
public Result $result,
26+
) {
2727
}
2828

2929
/**
@@ -32,7 +32,7 @@ public function __construct(Result $result)
3232
public function envelope(): Envelope
3333
{
3434
return new Envelope(
35-
subject: 'Speedtest Result #'.$this->result->id.' - Completed',
35+
subject: 'Speedtest Completed - #'.$this->result->id,
3636
);
3737
}
3838

@@ -45,16 +45,14 @@ public function content(): Content
4545
markdown: 'emails.speedtest-completed',
4646
with: [
4747
'id' => $this->result->id,
48+
'service' => Str::title($this->result->service),
49+
'serverName' => $this->result->server_name,
50+
'serverId' => $this->result->server_id,
51+
'ping' => round($this->result->ping, 2).' ms',
52+
'download' => Number::toBitRate(bits: $this->result->download_bits, precision: 2),
53+
'upload' => Number::toBitRate(bits: $this->result->upload_bits, precision: 2),
4854
'url' => url('/admin/results'),
4955
],
5056
);
5157
}
52-
53-
/**
54-
* Get the attachments for the message.
55-
*/
56-
public function attachments(): array
57-
{
58-
return [];
59-
}
6058
}

0 commit comments

Comments
 (0)