2025-06-05 11:15:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2025-09-04 11:57:38 +08:00
|
|
|
use App\Models\User;
|
2025-06-05 11:15:24 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
use App\Services\ApiClient;
|
|
|
|
|
|
|
|
class MachineStatusForwarder
|
|
|
|
{
|
|
|
|
protected string $externalUrl;
|
|
|
|
protected string $endpoint;
|
|
|
|
protected array $validated;
|
2025-09-04 11:57:38 +08:00
|
|
|
protected ?string $apiPlainToken;
|
2025-06-05 11:15:24 +08:00
|
|
|
|
2025-09-04 11:57:38 +08:00
|
|
|
public function __construct(string $externalUrl, string $endpoint, array $validated, ?string $token=null)
|
2025-06-05 11:15:24 +08:00
|
|
|
{
|
|
|
|
$this->externalUrl = $externalUrl;
|
|
|
|
$this->endpoint = $endpoint;
|
|
|
|
$this->validated = $validated;
|
2025-09-04 11:57:38 +08:00
|
|
|
$this->apiPlainToken = $token;
|
2025-06-05 11:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function forward(): ?Response
|
|
|
|
{
|
|
|
|
$response = null;
|
|
|
|
$parsed = parse_url($this->externalUrl);
|
|
|
|
$hostParts = explode('.', $parsed['host']);
|
|
|
|
|
2025-06-05 21:51:27 +08:00
|
|
|
$mainDomain = implode('.', array_slice($hostParts, 1));
|
2025-06-05 11:15:24 +08:00
|
|
|
|
|
|
|
$mainDomainUrl = "{$parsed['scheme']}://{$mainDomain}";
|
2025-09-04 11:57:38 +08:00
|
|
|
$token = $this->apiPlainToken ?? User::find(2)?->api_plain_token;
|
2025-09-02 11:25:28 +08:00
|
|
|
if ($token) {
|
|
|
|
$client = new ApiClient($mainDomainUrl, $token);
|
2025-06-05 11:15:24 +08:00
|
|
|
$response = $client->post($this->endpoint, $this->validated);
|
|
|
|
|
2025-06-05 11:52:18 +08:00
|
|
|
/* Log::info('✅ Machine status forwarded', [
|
2025-06-05 11:15:24 +08:00
|
|
|
'endpoint' => $this->endpoint,
|
|
|
|
'request' => $this->validated,
|
|
|
|
'status' => $response->status(),
|
|
|
|
'body' => $response->json(),
|
2025-06-05 11:52:18 +08:00
|
|
|
]); */
|
2025-06-05 11:15:24 +08:00
|
|
|
} else {
|
|
|
|
Log::warning("🔒 User with ID 2 not found or missing token");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|