forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetExternalIpAddress.php
More file actions
39 lines (31 loc) · 956 Bytes
/
GetExternalIpAddress.php
File metadata and controls
39 lines (31 loc) · 956 Bytes
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
<?php
namespace App\Actions;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;
use Throwable;
class GetExternalIpAddress
{
use AsAction;
public function handle(?string $url = null): array
{
$url = $url ?? config('speedtest.preflight.external_ip_url');
try {
$response = Http::retry(3, 100)
->timeout(5)
->get(url: $url);
} catch (Throwable $e) {
$message = sprintf('Failed to fetch external IP address from "%s". See the logs for more details.', $url);
Log::error($message, [$e->getMessage()]);
return [
'ok' => false,
'body' => $message,
];
}
return [
'ok' => $response->ok(),
'body' => Str::of($response->body())->trim()->toString(),
];
}
}