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