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
+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,
]);
}
}