74 lines
3.0 KiB
PHP
74 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Booking\Traits;
|
|
|
|
use App\Exception\BookingSessionNotFoundException;
|
|
use App\Exception\HotelNotFoundException;
|
|
use App\Exception\HotelNotInTravelException;
|
|
use App\Exception\NoRoomsAvailableException;
|
|
use App\Exception\TravelNotFoundException;
|
|
use App\Form\Model\BookingDto;
|
|
use App\Service\BookingSessionService;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Provides centralized exception handling for booking controllers.
|
|
*
|
|
* This trait contains common exception handling logic for booking operations
|
|
* including user-friendly error messages and appropriate redirects.
|
|
*/
|
|
trait BookingExceptionHandlerTrait
|
|
{
|
|
/**
|
|
* Safely retrieves booking DTO with centralized exception handling.
|
|
*
|
|
* Handles all booking-related exceptions and provides appropriate user feedback
|
|
* by redirecting to the error page with flash messages.
|
|
*/
|
|
protected function getOrCreateBookingCreateDto(BookingSessionService $bookingSessionService, Request $request): BookingDto|RedirectResponse
|
|
{
|
|
try {
|
|
return $bookingSessionService->getOrCreateBookingCreateDto($request);
|
|
} catch (BookingSessionNotFoundException $e) {
|
|
$this->addFlash('error', 'Deine Buchungssitzung ist abgelaufen. Bitte starte eine neue Buchung.');
|
|
|
|
return $this->redirectToRoute('app_booking_create_error');
|
|
} catch (TravelNotFoundException $e) {
|
|
$this->addFlash('error', 'Die angeforderte Reise wurde nicht gefunden.');
|
|
|
|
return $this->redirectToRoute('app_booking_create_error');
|
|
} catch (HotelNotFoundException $e) {
|
|
$this->addFlash('error', 'Das angeforderte Hotel wurde nicht gefunden.');
|
|
|
|
return $this->redirectToRoute('app_booking_create_error');
|
|
} catch (HotelNotInTravelException $e) {
|
|
$this->addFlash('error', 'Das Hotel ist für diese Reise nicht verfügbar.');
|
|
|
|
return $this->redirectToRoute('app_booking_create_error');
|
|
} catch (NoRoomsAvailableException $e) {
|
|
$this->addFlash('error', 'Für diese Reise sind aktuell keine Zimmer verfügbar.');
|
|
|
|
return $this->redirectToRoute('app_booking_create_error');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Safely retrieves booking DTO for HTMX requests with lightweight error responses.
|
|
*
|
|
* Returns empty 400 responses for HTMX requests when exceptions occur,
|
|
* allowing the frontend to handle errors appropriately.
|
|
*/
|
|
protected function getOrCreateBookingCreateDtoForHtmx(BookingSessionService $bookingSessionService, Request $request): mixed
|
|
{
|
|
try {
|
|
return $bookingSessionService->getOrCreateBookingCreateDto($request);
|
|
} catch (BookingSessionNotFoundException|TravelNotFoundException|HotelNotFoundException|HotelNotInTravelException|NoRoomsAvailableException $e) {
|
|
return new Response('', 400);
|
|
}
|
|
}
|
|
}
|