KTVCentral/app/Services/MachineStatusForwarder.php

50 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
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;
protected string $apiPlainToken;
public function __construct(string $externalUrl, string $endpoint, array $validated, string $token=null)
{
$this->externalUrl = $externalUrl;
$this->endpoint = $endpoint;
$this->validated = $validated;
$this->$apiPlainToken = $token;
}
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));
$mainDomainUrl = "{$parsed['scheme']}://{$mainDomain}";
$token = $this->api_plain_token ?? User::find(2)?->api_plain_token;
if ($token) {
$client = new ApiClient($mainDomainUrl, $token);
$response = $client->post($this->endpoint, $this->validated);
/* Log::info('✅ Machine status forwarded', [
'endpoint' => $this->endpoint,
'request' => $this->validated,
'status' => $response->status(),
'body' => $response->json(),
]); */
} else {
Log::warning("🔒 User with ID 2 not found or missing token");
}
return $response;
}
}