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; } }