chore: code cleanup

This commit is contained in:
Björn Fromme
2025-01-08 17:26:25 +01:00
parent b5eeeda3fa
commit e588716046
3 changed files with 50 additions and 31 deletions
+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');