chore: code cleanup
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Controller\Api;
|
||||
use App\BusProNet\XmlLoader\HotelXmlLoader;
|
||||
use App\BusProNet\XmlLoader\PickupXmlLoader;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -17,9 +18,9 @@ class TravelController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TravelXmlLoader $travelXmlLoader,
|
||||
private readonly HotelXmlLoader $hotelXmlLoader,
|
||||
private readonly HotelXmlLoader $hotelXmlLoader,
|
||||
private readonly PickupXmlLoader $pickupXmlLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly CacheInterface $cache,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -39,20 +40,24 @@ class TravelController extends AbstractController
|
||||
public function single(int $travelId, ?int $hotelId = null): JsonResponse
|
||||
{
|
||||
$cacheKey = sprintf('bpn_travel_%d_%d', $travelId, $hotelId ?? 0);
|
||||
$travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) {
|
||||
$item->expiresAfter(60);
|
||||
try {
|
||||
$travel = $this->cache->get($cacheKey, function (ItemInterface $item) use ($travelId, $hotelId) {
|
||||
$item->expiresAfter(60);
|
||||
|
||||
$travel = $this->travelXmlLoader->loadById($travelId, $hotelId);
|
||||
$travel = $this->travelXmlLoader->loadById($travelId, $hotelId);
|
||||
|
||||
if (null === $travel) {
|
||||
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
if (null === $travel) {
|
||||
return new JsonResponse(['message' => 'Not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$this->pickupXmlLoader->patchPickupsDetails($travel);
|
||||
$this->hotelXmlLoader->patchHotelDetails($travel);
|
||||
$this->pickupXmlLoader->patchPickupsDetails($travel);
|
||||
$this->hotelXmlLoader->patchHotelDetails($travel);
|
||||
|
||||
return $travel;
|
||||
});
|
||||
return $travel;
|
||||
});
|
||||
} catch (InvalidArgumentException $e) {
|
||||
$travel = null;
|
||||
}
|
||||
|
||||
return $this->json($travel, Response::HTTP_OK, [], ['groups' => ['api:list', 'api:single']]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user