forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingHostname.php
More file actions
50 lines (40 loc) · 1.22 KB
/
PingHostname.php
File metadata and controls
50 lines (40 loc) · 1.22 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
<?php
namespace App\Actions;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Ping\Ping;
use Spatie\Ping\PingResult;
use Throwable;
class PingHostname
{
use AsAction;
/**
* Attempt to ping the given hostname. Returns null when the ping binary
* is unavailable or another OS-level error prevents execution.
*/
public function handle(?string $hostname = null, int $count = 1): ?PingResult
{
$hostname = $hostname ?? config('speedtest.preflight.internet_check_hostname');
// Remove protocol if present
$hostname = preg_replace('#^https?://#', '', $hostname);
try {
$ping = (new Ping(
hostname: $hostname,
count: $count,
))->run();
} catch (Throwable $e) {
Log::debug('Ping command unavailable', [
'host' => $hostname,
'error' => $e->getMessage(),
]);
return null;
}
$data = $ping->toArray();
unset($data['raw_output'], $data['lines']);
Log::debug('Pinged hostname', [
'host' => $hostname,
'data' => $data,
]);
return $ping;
}
}