57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\BpnConnect;
|
|
|
|
use App\BpnConnect\Exception\BpnConnectException;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
abstract class AbstractApiClient
|
|
{
|
|
public function __construct(
|
|
protected readonly HttpClientInterface $httpClient,
|
|
protected readonly LoggerInterface $logger,
|
|
protected readonly ?string $baseUrl = null,
|
|
protected readonly ?string $apiKey = null,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $query
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function request(string $path, array $query = []): array
|
|
{
|
|
$this->assertConfigured();
|
|
|
|
try {
|
|
$response = $this->httpClient->request('GET', $this->baseUrl.$path, [
|
|
'headers' => [
|
|
'X-API-KEY' => $this->apiKey,
|
|
],
|
|
'query' => $query,
|
|
]);
|
|
|
|
return $response->toArray();
|
|
} catch (ExceptionInterface $e) {
|
|
$this->logger->error('BpnConnect request failed', [
|
|
'path' => $path,
|
|
'query' => $query,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
throw new BpnConnectException('BpnConnect request failed: '.$e->getMessage(), 0, $e);
|
|
}
|
|
}
|
|
|
|
private function assertConfigured(): void
|
|
{
|
|
if (null === $this->baseUrl || null === $this->apiKey) {
|
|
throw new BpnConnectException('BpnConnect client is not configured (missing BPN_CONNECT_BASE_URL or BPN_CONNECT_API_KEY).');
|
|
}
|
|
}
|
|
}
|