243 lines
9.6 KiB
PHP
243 lines
9.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Booking\Create;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Exception\TimeoutException;
|
|
use App\BusProNet\Model\Notification;
|
|
use App\Entity\User;
|
|
use App\Exception\BookingSessionNotFoundException;
|
|
use App\Exception\HotelNotInTravelException;
|
|
use App\Exception\NewsletterProviderException;
|
|
use App\Exception\TravelNotFoundException;
|
|
use App\Form\BookingCreateStep4Type;
|
|
use App\Form\Model\BookingDto;
|
|
use App\Form\Model\ParticipantDto;
|
|
use App\Htmx\HxTrait;
|
|
use App\Service\BookingConfigurator;
|
|
use App\Service\BookingCreateContextFactory;
|
|
use App\Service\BookingSessionManager;
|
|
use App\Service\NewsletterManager;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
|
|
|
/**
|
|
* Handles the fourth step of the booking creation process (confirmation).
|
|
*/
|
|
class Step4Controller extends AbstractBookingCreateController
|
|
{
|
|
use HxTrait;
|
|
|
|
public function __construct(
|
|
private readonly BookingConfigurator $bookingService,
|
|
private readonly BookingSessionManager $bookingSessionService,
|
|
private readonly BookingCreateContextFactory $createContextFactory,
|
|
private readonly ApiClient $apiClient,
|
|
private readonly CacheInterface $cache,
|
|
private readonly NewsletterManager $doubleOptInService,
|
|
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
|
|
{
|
|
try {
|
|
$bookingCreateDto = $this->loadBookingCreateDto($this->bookingSessionService, $request);
|
|
} catch (BookingSessionNotFoundException|TravelNotFoundException|HotelNotInTravelException $exception) {
|
|
return $this->createBookingCreateFailureResponse($exception, false);
|
|
}
|
|
|
|
// Validate step access
|
|
if (null !== $redirect = $this->validateStepAccess($bookingCreateDto, 4)) {
|
|
return $redirect;
|
|
}
|
|
|
|
$newsletterTargetEmail = $this->resolveNewsletterTargetEmail($bookingCreateDto);
|
|
$newsletterOptInVisible = null !== $newsletterTargetEmail;
|
|
|
|
$form = $this->createForm(BookingCreateStep4Type::class, $bookingCreateDto, [
|
|
'show_newsletter_opt_in' => $newsletterOptInVisible,
|
|
'newsletter_target_email' => $newsletterTargetEmail,
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if (true === $form->isSubmitted() && true === $form->isValid()) {
|
|
try {
|
|
$this->bookingService->applyCreateBookingStatusRules($bookingCreateDto);
|
|
|
|
// Submit final booking (already validated in Step 3)
|
|
$bookingResponse = $this->apiClient->createBooking($bookingCreateDto);
|
|
|
|
if ($bookingResponse instanceof Notification) {
|
|
$this->handleApiError(
|
|
$this->logger,
|
|
'Booking creation failed - API notification',
|
|
['message' => $bookingResponse->message],
|
|
$bookingResponse->message ?? 'Ein Fehler ist aufgetreten. Bitte versuchen es erneut.',
|
|
);
|
|
|
|
return $this->renderStepForm($bookingCreateDto, $form, $newsletterOptInVisible, $newsletterTargetEmail);
|
|
}
|
|
|
|
if (false === $bookingResponse->isBookingSuccessful()) {
|
|
$errorMessage = 'Buchung konnte nicht erstellt werden.';
|
|
if (null !== $bookingResponse->message && '' !== trim($bookingResponse->message)) {
|
|
$errorMessage .= ' '.$bookingResponse->message;
|
|
}
|
|
|
|
$this->handleApiError(
|
|
$this->logger,
|
|
'Booking creation unsuccessful',
|
|
['status' => $bookingResponse->status, 'message' => $bookingResponse->message],
|
|
$errorMessage,
|
|
);
|
|
|
|
return $this->renderStepForm($bookingCreateDto, $form, $newsletterOptInVisible, $newsletterTargetEmail);
|
|
}
|
|
|
|
$newsletterOptInSelected = $newsletterOptInVisible
|
|
&& $form->has('newsletterOptIn')
|
|
&& true === $form->get('newsletterOptIn')->getData();
|
|
|
|
if (true === $newsletterOptInSelected) {
|
|
try {
|
|
$targetParticipant = $this->resolveNewsletterTargetParticipant($bookingCreateDto);
|
|
$this->doubleOptInService->requestDefaultListSubscription(
|
|
$newsletterTargetEmail,
|
|
$targetParticipant?->firstName,
|
|
$targetParticipant?->lastName,
|
|
);
|
|
} catch (NewsletterProviderException|\InvalidArgumentException $e) {
|
|
$this->logger->warning('Newsletter confirmation request failed after booking', [
|
|
'email' => $newsletterTargetEmail,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Success: Store booking data in flash for conversion tracking
|
|
$bookingCreateContext = $this->createContextFactory->createWithParticipantPrices($bookingCreateDto);
|
|
$this->addFlash('booking_number', $bookingResponse->bookingNumber);
|
|
$this->addFlash('booking_total', $bookingCreateContext->summaryData->vouchers->payableAmount);
|
|
$this->addFlash('booking_travel_name', $bookingCreateDto->travel->label);
|
|
$this->clearTravelDataCache($bookingCreateDto);
|
|
$this->bookingSessionService->clearBookingDto($request, BookingDto::MODE_CREATE);
|
|
|
|
$this->logger->info('Booking successfully created.', [
|
|
'date_id' => $bookingCreateDto->travel->id,
|
|
'hotel_id' => $bookingCreateDto->hotelId,
|
|
'booking_number' => $bookingResponse->bookingNumber,
|
|
]);
|
|
|
|
return $this->redirectToRoute('app_booking_create_success');
|
|
} catch (TimeoutException $e) {
|
|
$this->handleApiError(
|
|
$this->logger,
|
|
'Booking creation timeout',
|
|
[
|
|
'exception' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
],
|
|
'Die Anfrage hat zu lange gedauert. Bitte versuche es erneut.',
|
|
);
|
|
} catch (\Exception $e) {
|
|
$this->handleApiError(
|
|
$this->logger,
|
|
'Booking creation exception',
|
|
[
|
|
'exception' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
],
|
|
'Ein technischer Fehler ist aufgetreten.',
|
|
);
|
|
}
|
|
}
|
|
|
|
return $this->renderStepForm($bookingCreateDto, $form, $newsletterOptInVisible, $newsletterTargetEmail);
|
|
}
|
|
|
|
/**
|
|
* Renders the step 4 form with standard template variables.
|
|
*/
|
|
/**
|
|
* @param FormInterface<mixed> $form
|
|
*/
|
|
private function renderStepForm(
|
|
BookingDto $bookingCreateDto,
|
|
FormInterface $form,
|
|
bool $newsletterOptInVisible,
|
|
?string $newsletterTargetEmail,
|
|
): Response {
|
|
$bookingCreateContext = $this->createContextFactory->createWithParticipantPrices($bookingCreateDto);
|
|
|
|
return $this->render('booking/create/step_4.html.twig', [
|
|
'bookingCreateContext' => $bookingCreateContext,
|
|
'form' => $form->createView(),
|
|
'newsletterOptInVisible' => $newsletterOptInVisible,
|
|
'newsletterTargetEmail' => $newsletterTargetEmail,
|
|
]);
|
|
}
|
|
|
|
private function resolveNewsletterTargetEmail(BookingDto $bookingDto): ?string
|
|
{
|
|
$currentUser = $this->getUser();
|
|
$email = null;
|
|
|
|
if ($currentUser instanceof User) {
|
|
$email = $currentUser->getEmail();
|
|
}
|
|
|
|
if ((null === $email || '' === trim((string) $email)) && isset($bookingDto->participants[0])) {
|
|
$email = $bookingDto->participants[0]->email;
|
|
}
|
|
|
|
if (null === $email) {
|
|
return null;
|
|
}
|
|
|
|
$normalizedEmail = mb_strtolower(trim($email));
|
|
|
|
return false !== filter_var($normalizedEmail, FILTER_VALIDATE_EMAIL) ? $normalizedEmail : null;
|
|
}
|
|
|
|
private function resolveNewsletterTargetParticipant(BookingDto $bookingDto): ?ParticipantDto
|
|
{
|
|
$participant = $bookingDto->participants[0] ?? null;
|
|
if (null === $participant) {
|
|
return null;
|
|
}
|
|
|
|
if (null === $participant->firstName && null === $participant->lastName) {
|
|
return null;
|
|
}
|
|
|
|
return $participant;
|
|
}
|
|
|
|
/**
|
|
* Clears travel data and availability cache after successful booking.
|
|
*/
|
|
private function clearTravelDataCache(BookingDto $bookingDto): void
|
|
{
|
|
$dateId = $bookingDto->travel->id;
|
|
$hotelId = $bookingDto->hotelId;
|
|
|
|
// Clear availability cache
|
|
$this->cache->delete(sprintf('availability_%d', $dateId));
|
|
|
|
// Clear travel data cache (both local and remote variants)
|
|
$this->cache->delete(sprintf('travel_unified_%d_%d_local', $dateId, $hotelId));
|
|
$this->cache->delete(sprintf('travel_unified_%d_%d_remote', $dateId, $hotelId));
|
|
}
|
|
}
|