forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnhealthySpeedtestMail.php
More file actions
83 lines (72 loc) · 2.31 KB
/
UnhealthySpeedtestMail.php
File metadata and controls
83 lines (72 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace App\Mail;
use App\Helpers\Number;
use App\Models\Result;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class UnhealthySpeedtestMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(
public Result $result,
) {}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Speedtest Threshold Breached - #'.$this->result->id,
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
$benchmarks = [];
foreach ($this->result->benchmarks as $metric => $benchmark) {
$benchmarks[] = $this->formatBenchmark($metric, $benchmark);
}
return new Content(
markdown: 'mail.speedtest.unhealthy',
with: [
'id' => $this->result->id,
'service' => str($this->result->service->getLabel())->title(),
'isp' => $this->result->isp,
'url' => url('/admin/results'),
'benchmarks' => $benchmarks,
],
);
}
/**
* Format a benchmark for display in the email.
*/
private function formatBenchmark(string $metric, array $benchmark): array
{
$metricName = str($metric)->title();
$type = str($benchmark['type'])->title();
$thresholdValue = $benchmark['value'].' '.str($benchmark['unit'])->title();
// Get the actual result value
$resultValue = match ($metric) {
'download' => Number::toBitRate($this->result->download_bits, 2),
'upload' => Number::toBitRate($this->result->upload_bits, 2),
'ping' => round(Number::castToType($this->result->ping, 'float'), 2).' ms',
default => 'N/A',
};
return [
'metric' => $metricName,
'type' => $type,
'threshold_value' => $thresholdValue,
'result_value' => $resultValue,
'passed' => $benchmark['passed'],
];
}
}