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']]);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\BusProNet\XmlLoader\PickupXmlLoader;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
use App\Form\BookingType;
|
||||
use App\Form\Model\BookingData;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -19,11 +20,11 @@ use Symfony\Contracts\Cache\ItemInterface;
|
||||
class EditController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelXmlLoader $travelDataLoader,
|
||||
private readonly PickupXmlLoader $pickupDataLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -40,11 +41,15 @@ class EditController extends AbstractController
|
||||
// Fetch original bookingData data via API and cache result for a short ttl
|
||||
$cacheKeyList = sprintf('bpn_bookings_%s', sha1($bpnUser->getUserIdentifier()));
|
||||
$cacheKeySingle = sprintf('bpn_booking_%d', $id);
|
||||
$bookingData = $this->cache->get($cacheKeySingle, function (ItemInterface $item) use ($bpnUser, $id) {
|
||||
$item->expiresAfter(300);
|
||||
try {
|
||||
$bookingData = $this->cache->get($cacheKeySingle, function (ItemInterface $item) use ($bpnUser, $id) {
|
||||
$item->expiresAfter(300);
|
||||
|
||||
return $this->apiClient->getBooking($bpnUser->getEmail(), $bpnUser->getPassword(), $id);
|
||||
});
|
||||
return $this->apiClient->getBooking($bpnUser->getEmail(), $bpnUser->getPassword(), $id);
|
||||
});
|
||||
} catch (InvalidArgumentException $e) {
|
||||
$bookingData = null;
|
||||
}
|
||||
|
||||
if (null === $bookingData) {
|
||||
$this->addFlash('error', 'Buchungsdaten nicht (mehr) verfügbar');
|
||||
@@ -86,8 +91,11 @@ class EditController extends AbstractController
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->apiClient->updateBooking($formData, false);
|
||||
|
||||
$this->cache->delete($cacheKeySingle);
|
||||
$this->cache->delete($cacheKeyList);
|
||||
try {
|
||||
$this->cache->delete($cacheKeySingle);
|
||||
$this->cache->delete($cacheKeyList);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
}
|
||||
|
||||
$this->addFlash('success', 'Buchung erfolgreich aktualisiert');
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Controller\Booking;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\XmlLoader\TravelXmlLoader;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -16,10 +17,10 @@ use Symfony\Contracts\Cache\ItemInterface;
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly TravelXmlLoader $travelDataLoader,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -35,17 +36,22 @@ class IndexController extends AbstractController
|
||||
|
||||
// Cache bookings for a short ttl
|
||||
$cacheKey = sprintf('bpn_bookings_%s', sha1($bpnUser->getUserIdentifier()));
|
||||
$bookings = $this->cache->get($cacheKey, function (ItemInterface $item) use ($bpnUser) {
|
||||
$item->expiresAfter(300);
|
||||
try {
|
||||
$bookings = $this->cache->get($cacheKey, function (ItemInterface $item) use ($bpnUser) {
|
||||
$item->expiresAfter(300);
|
||||
|
||||
$bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
|
||||
$this->travelDataLoader->patchBookings($bookings);
|
||||
$bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
|
||||
$this->travelDataLoader->patchBookings($bookings);
|
||||
|
||||
return $bookings;
|
||||
});
|
||||
return $bookings;
|
||||
});
|
||||
$items = $bookings->getItems();
|
||||
} catch (InvalidArgumentException $e) {
|
||||
$items = [];
|
||||
}
|
||||
|
||||
return $this->render('booking/index.html.twig', [
|
||||
'bookings' => $bookings->getItems(),
|
||||
'bookings' => $items,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user