123 lines
4.5 KiB
PHP
123 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Api;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Exception\ApiClientException;
|
|
use App\BusProNet\Model\Travel;
|
|
use App\BusProNet\Utility\DateCodeUtility;
|
|
use App\Service\TravelDataService;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Attribute\MapDateTime;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
#[Route('/api')]
|
|
#[IsGranted('ROLE_OAUTH2_API')]
|
|
class TravelController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly ApiClient $apiClient,
|
|
private readonly TravelDataService $travelDataService,
|
|
) {
|
|
}
|
|
|
|
#[Route(path: '/travels', name: 'api_travel_mapping')]
|
|
public function mapping(): JsonResponse
|
|
{
|
|
$mapping = $this->travelDataService->generateFilesMap();
|
|
|
|
return $this->json($mapping);
|
|
}
|
|
|
|
#[Route(
|
|
path: '/travels/{dateId}/{hotelId}',
|
|
name: 'api_travel_single_id',
|
|
requirements: ['dateId' => '\d+', 'hotelId' => '\d+'],
|
|
defaults: ['hotelId' => null],
|
|
)]
|
|
public function byId(Request $request, int $dateId, ?int $hotelId = null): JsonResponse
|
|
{
|
|
$source = $request->query->get('source');
|
|
$preferRemote = $request->query->getBoolean('prefer_remote');
|
|
|
|
$travel = $this->loadTravelData($dateId, $hotelId, $source, $preferRemote);
|
|
|
|
if (null === $travel) {
|
|
return $this->json(['message' => 'Travel not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
|
}
|
|
|
|
#[Route(
|
|
path: '/travels/{dateCode}/{hotelCode}',
|
|
name: 'api_travel_single_code',
|
|
defaults: ['hotelCode' => null],
|
|
)]
|
|
public function byCode(Request $request, $dateCode, ?string $hotelCode = null): JsonResponse
|
|
{
|
|
// sanitize date code by removing potential dividers
|
|
$dateCode = (new DateCodeUtility())->sanitize($dateCode);
|
|
|
|
$dateId = $this->travelDataService->mapDateCodeToId($dateCode);
|
|
$hotelId = $hotelCode ? $this->travelDataService->mapHotelCodeToId($hotelCode) : null;
|
|
|
|
if (null === $dateId) {
|
|
return $this->json(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
$source = $request->query->get('source');
|
|
$preferRemote = $request->query->getBoolean('prefer_remote');
|
|
|
|
$travel = $this->loadTravelData($dateId, $hotelId, $source, $preferRemote);
|
|
|
|
if (null === $travel) {
|
|
return $this->json(['message' => 'Travel not found'], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
|
}
|
|
|
|
#[Route('/travels/{dateId}/{hotelId}/{dateTo}/availability', name: 'api_travel_hotel_availability')]
|
|
public function hotelAvailability(
|
|
int $dateId,
|
|
int $hotelId,
|
|
#[MapDateTime(format: 'Y-m-d')] \DateTimeImmutable $dateTo,
|
|
): JsonResponse {
|
|
try {
|
|
$result = $this->apiClient->getHotelAvailability($dateId, $hotelId, $dateTo);
|
|
|
|
return $this->json($result);
|
|
} catch (ApiClientException $e) {
|
|
return $this->json(['error' => $e->getMessage(), 'type' => 'api_error']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Loads travel data based on the provided parameters.
|
|
*
|
|
* @param int $dateId the ID of the date for which travel data is requested
|
|
* @param int|null $hotelId the ID of the hotel for which travel data is requested (optional)
|
|
* @param string|null $source the source of the travel data ('local', 'remote', or null for default behavior)
|
|
* @param bool $preferRemote whether to prefer remote data when the source is not explicitly specified
|
|
*
|
|
* @return Travel|null returns a Travel object if data is found, or null if no data is available
|
|
*/
|
|
private function loadTravelData(
|
|
int $dateId,
|
|
?int $hotelId = null,
|
|
?string $source = null,
|
|
bool $preferRemote = false,
|
|
): ?Travel {
|
|
return match ($source) {
|
|
'local' => $this->travelDataService->getTravelDataFromXml($dateId, $hotelId),
|
|
'remote' => $this->travelDataService->getTravelDataFromApi($dateId, $hotelId),
|
|
default => $this->travelDataService->getTravelData($dateId, $hotelId, $preferRemote),
|
|
};
|
|
}
|
|
}
|