feat: refactor to cards
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Booking\Create;
|
||||
|
||||
use App\BusProNet\XmlLoader\AgencyLoader;
|
||||
use App\Exception\BookingNotPossibleException;
|
||||
use App\Exception\HotelNotFoundException;
|
||||
use App\Exception\HotelNotInTravelException;
|
||||
use App\Exception\NoRoomsAvailableException;
|
||||
use App\Exception\TravelNotFoundException;
|
||||
use App\Service\BookingService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
* Handles the initialization of new booking sessions.
|
||||
*
|
||||
* This controller provides a clean entry point for starting new booking flows
|
||||
* without requiring random UID parameters. It creates fresh booking sessions
|
||||
* and redirects to the first step of the booking process.
|
||||
*/
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BookingService $bookingService,
|
||||
private readonly AgencyLoader $agencyLoader,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a fresh booking session and redirects to step 1.
|
||||
*
|
||||
* This endpoint provides a clean way to start the booking flow with just
|
||||
* dateId and hotelId parameters. It clears any existing booking session
|
||||
* and creates a fresh BookingCreateDto before redirecting to step 1.
|
||||
*
|
||||
* Optionally accepts an agency code parameter. If provided and valid, the
|
||||
* corresponding agency ID is stored in the booking. If not provided or invalid,
|
||||
* defaults to agency code '0001'.
|
||||
*/
|
||||
#[Route(
|
||||
path: '/bookings/create/{dateId}/{hotelId}',
|
||||
name: 'app_booking_create_init',
|
||||
requirements: ['dateId' => '\d+', 'hotelId' => '\d+']
|
||||
)]
|
||||
public function init(Request $request, int $dateId, int $hotelId): Response
|
||||
{
|
||||
try {
|
||||
// Clear any existing booking session to ensure fresh start
|
||||
$this->bookingService->clearBookingSession($request);
|
||||
|
||||
// Determine agency ID from optional query parameter
|
||||
$agencyId = $this->resolveAgencyId($request->query->get('agency'));
|
||||
|
||||
// Create fresh booking session with the provided parameters
|
||||
$this->bookingService->startFreshBooking($request, $dateId, $hotelId, $agencyId);
|
||||
|
||||
// Redirect to step 1 of the booking flow
|
||||
return $this->redirectToRoute('app_booking_create_step_1');
|
||||
} catch (TravelNotFoundException $e) {
|
||||
throw $this->createNotFoundException(sprintf('Travel not found for date ID %d', $dateId));
|
||||
} catch (HotelNotFoundException $e) {
|
||||
throw $this->createNotFoundException(sprintf('Hotel not found for hotel ID %d', $hotelId));
|
||||
} catch (HotelNotInTravelException $e) {
|
||||
throw $this->createNotFoundException(sprintf('Hotel ID %d is not available for travel ID %d', $hotelId, $dateId));
|
||||
} catch (NoRoomsAvailableException $e) {
|
||||
throw $this->createNotFoundException('No rooms available for this travel.');
|
||||
} catch (BookingNotPossibleException $e) {
|
||||
throw $this->createNotFoundException('Booking is not possible for this travel (Buchungsstop).');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the agency ID from the provided agency code.
|
||||
*
|
||||
* If the code is null or the agency is not found, returns the default agency ID.
|
||||
*
|
||||
* @param string|null $agencyCode The agency code from the request parameter
|
||||
*
|
||||
* @return int|null The agency ID, or null if default agency not found
|
||||
*/
|
||||
private function resolveAgencyId(?string $agencyCode): ?int
|
||||
{
|
||||
// Use default agency if no code provided
|
||||
if (null === $agencyCode || '' === trim($agencyCode)) {
|
||||
$defaultAgency = $this->agencyLoader->loadDefault();
|
||||
|
||||
return $defaultAgency?->id;
|
||||
}
|
||||
|
||||
// Try to find agency by provided code
|
||||
$agency = $this->agencyLoader->loadByCode($agencyCode);
|
||||
|
||||
// Fall back to default agency if code not found
|
||||
if (null === $agency) {
|
||||
$defaultAgency = $this->agencyLoader->loadDefault();
|
||||
|
||||
return $defaultAgency?->id;
|
||||
}
|
||||
|
||||
return $agency->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays user-friendly error messages for booking initialization failures.
|
||||
*
|
||||
* This endpoint provides a centralized location for displaying booking errors
|
||||
* with appropriate error messages and guidance for users.
|
||||
*/
|
||||
#[Route('/bookings/create/error', name: 'app_booking_create_error')]
|
||||
public function error(Request $request): Response
|
||||
{
|
||||
return $this->render('booking/create/error.html.twig');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user