Files
myep/src/Controller/Booking/IndexController.php
T

63 lines
1.9 KiB
PHP

<?php
namespace App\Controller\Booking;
use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Model\BaseData;
use App\BusProNet\Model\Notification;
use App\BusProNet\XmlLoader\TravelLoader;
use App\Entity\User;
use App\Security\Crypt;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class IndexController extends AbstractController
{
public function __construct(
private readonly ApiClient $apiClient,
private readonly TravelLoader $travelDataLoader,
private readonly Crypt $crypt,
private readonly LoggerInterface $logger,
) {
}
#[Route('/bookings', name: 'app_bookings')]
#[IsGranted("ROLE_USER")]
public function index(Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
$email = $user->getEmail();
$password = $this->crypt->decrypt($user->getPassword());
try {
$bookings = $this->apiClient->getBookings($email, $password);
} catch (ApiClientException $e) {
$this->addFlash('error', 'Buchungen nicht abrufbar');
$bookings = new BaseData([]);
}
if ($bookings instanceof Notification) {
$this->logger->error('Unable to fetch bookings data', [
'email' => $email,
'code' => $bookings->code,
'error' => $bookings->message,
]);
$this->addFlash('error', 'Buchungen nicht abrufbar');
$bookings = new BaseData([]);
}
$this->travelDataLoader->patchBookings($bookings);
return $this->render('booking/index.html.twig', [
'bookings' => $bookings->getItems(),
]);
}
}