chore: code cleanup

This commit is contained in:
Björn Fromme committed 2025-01-08 17:26:25 +01:00
1 parent b5eeeda3fa
commit e588716046
3 files changed
+50 -31

No files matched your search

+17 -9
View File
@@ -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');
+16 -10
View File
@@ -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,
]);
}
}