Files
oficial/cli/src/Service/FastCGI.php
Juan Pablo Vial d4f8804fbb Batch queue
2025-06-30 15:52:28 -04:00

121 lines
3.7 KiB
PHP

<?php
namespace Incoviba\Service;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use hollodotme\FastCGI as FCGI;
use Incoviba\Exception\Client\FastCGI as FastCGIException;
class FastCGI implements LoggerAwareInterface
{
public function __construct(protected Login $loginService, protected string $hostname, protected int $port,
protected string $documentRoot,
protected int $connectionTimeout = 5000, protected int $readTimeout = 5000)
{
$this->client = new FCGI\Client();
}
public LoggerInterface $logger;
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
protected FCGI\Client $client;
protected FCGI\Interfaces\ConfiguresSocketConnection $socket;
public function connect(): self
{
$this->socket = new FCGI\SocketConnections\NetworkSocket($this->hostname, $this->port, $this->connectionTimeout, $this->readTimeout);
return $this;
}
protected array $socketIds = [];
/**
* @throws FastCGIException
*/
public function sendRequest(FCGI\Interfaces\ProvidesRequestData $request): self
{
if (!isset($this->socket)) {
$this->connect();
}
$request = $this->setHeaders($request);
try {
$this->socketIds []= $this->client->sendAsyncRequest($this->socket, $request);
} catch (FCGI\Exceptions\FastCGIClientException $exception) {
$this->logger->error($exception->getMessage());
throw new FastCGIException($exception);
}
return $this;
}
/**
* @return array
*/
public function awaitResponses(): array
{
$responses = [];
$repeats = 0;
$maxRepeats = count($this->socketIds);
while ($this->client->hasUnhandledResponses()) {
if ($repeats >= $maxRepeats) {
break;
}
try {
$readyResponses = $this->client->readReadyResponses(3000);
} catch (FCGI\Exceptions\FastCGIClientException $exception) {
$this->logger->error($exception->getMessage());
$repeats ++;
continue;
}
foreach ($readyResponses as $response) {
$responses []= $response;
$repeats ++;
}
}
if ($this->client->hasUnhandledResponses()) {
$this->logger->error("Unhandled responses");
return array_merge($responses, $this->awaitResponses());
}
return $responses;
}
/**
* @param string $uri
* @return FastCGI
* @throws FastCGIException
*/
public function get(string $uri): self
{
$request = new FCGI\Requests\GetRequest($this->documentRoot, '');
$request->setRequestUri($uri);
return $this->sendRequest($request);
}
/**
* @param string $uri
* @param ?array $data
* @return FastCGI
* @throws FastCGIException
*/
public function post(string $uri, ?array $data): self
{
$content = new FCGI\RequestContents\JsonData($data ?? []);
$request = FCGI\Requests\PostRequest::newWithRequestContent($this->documentRoot, $content);
$request->setRequestUri($uri);
return $this->sendRequest($request);
}
protected function setHeaders(FCGI\Interfaces\ProvidesRequestData $request): FCGI\Interfaces\ProvidesRequestData
{
$apiKey = $this->loginService->getKey();
$request->setCustomVar('HTTP_AUTHORIZATION', "Bearer {$apiKey}");
return $request;
}
}