forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendWebhookTestNotification.php
More file actions
56 lines (48 loc) · 1.95 KB
/
SendWebhookTestNotification.php
File metadata and controls
56 lines (48 loc) · 1.95 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
<?php
namespace App\Actions\Notifications;
use App\Helpers\Number;
use App\Models\Result;
use App\Services\SpeedtestFakeResultGenerator;
use Filament\Notifications\Notification;
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\WebhookServer\WebhookCall;
class SendWebhookTestNotification
{
use AsAction;
public function handle(array $webhooks)
{
if (! count($webhooks)) {
Notification::make()
->title(__('settings/notifications.test_notifications.webhook.add'))
->warning()
->send();
return;
}
// Generate a fake Result (NOT saved to database)
$fakeResult = SpeedtestFakeResultGenerator::completed();
foreach ($webhooks as $webhook) {
WebhookCall::create()
->url($webhook['url'])
->payload([
'result_id' => Str::uuid(),
'site_name' => __('settings/notifications.test_notifications.webhook.payload'),
'server_name' => $fakeResult->data['server']['name'],
'server_id' => $fakeResult->data['server']['id'],
'isp' => $fakeResult->data['isp'],
'ping' => round($fakeResult->ping),
'download' => Number::bitsToMagnitude(bits: $fakeResult->upload, precision: 0, magnitude: 'mbit'),
'upload' => Number::bitsToMagnitude(bits: $fakeResult->download, precision: 0, magnitude: 'mbit'),
'packet_loss' => $fakeResult->data['packetLoss'],
'speedtest_url' => $fakeResult->data['result']['url'],
'url' => url('/admin/results'),
])
->doNotSign()
->dispatchSync();
}
Notification::make()
->title(__('settings/notifications.test_notifications.webhook.sent'))
->success()
->send();
}
}