140 lines
4.8 KiB
PHP
140 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Booking\Create;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Model\Notification;
|
|
use App\Controller\Booking\Traits\BookingCreateTrait;
|
|
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
|
|
use App\Form\BookingCreateStep4Type;
|
|
use App\Form\Model\BookingDto;
|
|
use App\Htmx\HxTrait;
|
|
use App\Service\BookingService;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
/**
|
|
* Handles the fourth step of the booking creation process (confirmation).
|
|
*/
|
|
class Step4Controller extends AbstractController
|
|
{
|
|
use BookingCreateTrait;
|
|
use BookingExceptionHandlerTrait;
|
|
use HxTrait;
|
|
|
|
public function __construct(
|
|
private readonly BookingService $bookingService,
|
|
private readonly ApiClient $apiClient,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Displays booking summary and confirmation form.
|
|
*/
|
|
#[Route('/bookings/create/confirmation', name: 'app_booking_create_step_4')]
|
|
public function index(Request $request): Response
|
|
{
|
|
$result = $this->getOrCreateBookingCreateDto($this->bookingService, $request);
|
|
if ($result instanceof Response) {
|
|
return $result;
|
|
}
|
|
$bookingCreateDto = $result;
|
|
|
|
// Validate step access
|
|
if ($redirect = $this->validateStepAccess($bookingCreateDto, 4)) {
|
|
return $redirect;
|
|
}
|
|
|
|
$form = $this->createForm(BookingCreateStep4Type::class, $bookingCreateDto, [
|
|
'attr' => [
|
|
'hx-post' => $this->generateUrl('app_booking_create_step_4'),
|
|
'hx-target' => '#form-wrapper',
|
|
'hx-select' => '#form-wrapper',
|
|
'hx-swap' => 'outerHTML',
|
|
],
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
try {
|
|
// Submit final booking (already validated in Step 3)
|
|
$bookingResponse = $this->apiClient->createBooking($bookingCreateDto);
|
|
|
|
if ($bookingResponse instanceof Notification) {
|
|
return $this->handleBookingError(
|
|
'Booking creation failed - API notification',
|
|
['message' => $bookingResponse->message],
|
|
$bookingResponse->message,
|
|
$bookingCreateDto,
|
|
$form
|
|
);
|
|
}
|
|
|
|
if (false === $bookingResponse->isBookingSuccessful()) {
|
|
return $this->handleBookingError(
|
|
'Booking creation unsuccessful',
|
|
['status' => $bookingResponse->status],
|
|
'Buchung konnte nicht erstellt werden.',
|
|
$bookingCreateDto,
|
|
$form
|
|
);
|
|
}
|
|
|
|
// Success: Store booking number in flash and clear session
|
|
$this->addFlash('booking_number', $bookingResponse->transactionNumber);
|
|
$this->bookingService->clearBookingCreateDto($request);
|
|
|
|
return $this->hxRedirect($request, $this->generateUrl('app_booking_create_success'));
|
|
} catch (\Exception $e) {
|
|
return $this->handleBookingError(
|
|
'Booking creation exception',
|
|
[
|
|
'exception' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
],
|
|
'Ein technischer Fehler ist aufgetreten.',
|
|
$bookingCreateDto,
|
|
$form
|
|
);
|
|
}
|
|
}
|
|
|
|
return $this->renderStepForm($bookingCreateDto, $form);
|
|
}
|
|
|
|
/**
|
|
* Handles booking errors by logging, adding flash message, and rendering the form.
|
|
*/
|
|
private function handleBookingError(
|
|
string $logMessage,
|
|
array $context,
|
|
string $flashMessage,
|
|
BookingDto $bookingCreateDto,
|
|
FormInterface $form,
|
|
): Response {
|
|
$this->logger->error($logMessage, $context);
|
|
$this->addFlash('error', $flashMessage);
|
|
|
|
return $this->renderStepForm($bookingCreateDto, $form);
|
|
}
|
|
|
|
/**
|
|
* Renders the step 4 form with standard template variables.
|
|
*/
|
|
private function renderStepForm(BookingDto $bookingCreateDto, FormInterface $form): Response
|
|
{
|
|
return $this->render('booking/create/step_4.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
}
|