60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
class CmsDataProvider
|
|
{
|
|
public function __construct(private readonly HttpClientInterface $httpClient, private readonly string $apiKey)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Fetches only the hotel images from the CMS.
|
|
*
|
|
* @return array<string, mixed>|null The images array or null if unavailable
|
|
*/
|
|
public function getProductImages(string $productCode, ?string $hotelCode = null): ?array
|
|
{
|
|
$result = $this->getProductDetails($productCode, $hotelCode);
|
|
|
|
if (true === isset($result['success']) && false === $result['success']) {
|
|
return null;
|
|
}
|
|
|
|
return $result['hotel']['images'] ?? null;
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function getProductDetails(string $productCode, ?string $hotelCode = null): array
|
|
{
|
|
try {
|
|
$request = $this->httpClient->request('GET', 'api/product', [
|
|
'query' => [
|
|
'product' => $productCode,
|
|
'hotel' => $hotelCode,
|
|
'key' => $this->apiKey,
|
|
],
|
|
]);
|
|
} catch (ExceptionInterface $e) {
|
|
return [
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
];
|
|
}
|
|
|
|
try {
|
|
$data = $request->toArray();
|
|
} catch (ExceptionInterface $e) {
|
|
return [
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|