feat: centralize create/edit flow contexts, extract edit submit service
This commit is contained in:
@@ -4,9 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Booking\Edit;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\Exception\ApiClientException;
|
||||
use App\BusProNet\Exception\TimeoutException;
|
||||
use App\BusProNet\Model\Notification;
|
||||
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
|
||||
use App\Entity\User;
|
||||
@@ -16,13 +13,10 @@ use App\Form\Model\BookingDto;
|
||||
use App\Htmx\HxTrait;
|
||||
use App\Service\BookingEditDataLoaderService;
|
||||
use App\Service\BookingEditDraftService;
|
||||
use App\Service\BookingEditContextFactory;
|
||||
use App\Service\BookingFingerprintService;
|
||||
use App\Service\BookingEditSubmitGuardService;
|
||||
use App\Service\BookingEditSubmitService;
|
||||
use App\Service\BookingSessionService;
|
||||
use App\Service\BookingSummaryDataService;
|
||||
use App\Service\ParticipantCardDataService;
|
||||
use App\Service\TravelDataService;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -36,7 +30,7 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
* - Card overview with lazy-loaded individual participant forms
|
||||
* - Handles canceled participants (status 'S')
|
||||
* - Applies mutability constraints via EditFieldStateProvider
|
||||
* - Final submission calls ApiClient::updateBooking()
|
||||
* - Final submission is delegated to BookingEditSubmitService
|
||||
*/
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
@@ -44,16 +38,12 @@ class IndexController extends AbstractController
|
||||
use HxTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly BookingEditDataLoaderService $dataLoader,
|
||||
private readonly BookingEditDraftService $draftService,
|
||||
private readonly TravelDataService $travelDataService,
|
||||
private readonly BookingSessionService $bookingSessionService,
|
||||
private readonly BookingEditSubmitGuardService $submitGuard,
|
||||
private readonly BookingFingerprintService $fingerprintService,
|
||||
private readonly BookingSummaryDataService $summaryDataService,
|
||||
private readonly ParticipantCardDataService $participantCardService,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly BookingEditContextFactory $editContextFactory,
|
||||
private readonly BookingEditSubmitService $submitService,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -114,34 +104,26 @@ class IndexController extends AbstractController
|
||||
return $this->redirectToRoute('app_bookings');
|
||||
}
|
||||
|
||||
// Fetch mutable data for form constraints
|
||||
$mutableData = $this->travelDataService->getMutabilityData($bookingData->dateId);
|
||||
|
||||
// Create validation form (same pattern as CreateStep2Controller)
|
||||
$form = $this->createForm(BookingEditType::class, $bookingDto);
|
||||
$form->handleRequest($request);
|
||||
|
||||
// Handle form submission (clicking "Buchung aktualisieren")
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
return $this->handleFormSubmission($request, $bookingDto, $id, $user);
|
||||
return $this->submitService->handleSubmission($request, $bookingDto, $id, $user);
|
||||
}
|
||||
|
||||
// Always generate card data with validation state to show completeness
|
||||
$cardsData = $this->participantCardService->getAllCardsDataWithValidation($bookingDto);
|
||||
|
||||
// Get complete summary data (pricing, rooms, CMS data)
|
||||
$summaryData = $this->summaryDataService->getSummaryData($bookingDto);
|
||||
$bookingEditContext = $this->editContextFactory->createOverviewContext(
|
||||
$bookingDto,
|
||||
$bookingData,
|
||||
$this->fingerprintService->isDirty($bookingDto),
|
||||
$form->isSubmitted(),
|
||||
$form->isSubmitted() && false === $form->isValid(),
|
||||
);
|
||||
|
||||
$templateData = [
|
||||
'form' => $form->createView(),
|
||||
'bookingDto' => $bookingDto,
|
||||
'bookingData' => $bookingData,
|
||||
'mutableData' => $mutableData,
|
||||
'cardsData' => $cardsData,
|
||||
'summaryData' => $summaryData,
|
||||
'isDirty' => $this->fingerprintService->isDirty($bookingDto),
|
||||
'isSubmitted' => $form->isSubmitted(),
|
||||
'hasValidationErrors' => $form->isSubmitted() && false === $form->isValid(),
|
||||
'bookingEditContext' => $bookingEditContext,
|
||||
];
|
||||
|
||||
return $this->render('booking/edit/index.html.twig', $templateData);
|
||||
@@ -208,97 +190,4 @@ class IndexController extends AbstractController
|
||||
|
||||
return $this->redirectToRoute('app_bookings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles form submission for booking update.
|
||||
*/
|
||||
private function handleFormSubmission(Request $request, BookingDto $bookingDto, int $id, User $user): Response
|
||||
{
|
||||
$email = $user->getEmail();
|
||||
|
||||
$this->logger->info('Initiated booking update', [
|
||||
'email' => $email,
|
||||
'booking_id' => $id,
|
||||
]);
|
||||
|
||||
$this->dataLoader->invalidateBookingCache($id, $user);
|
||||
$freshBookingData = $this->dataLoader->fetchBookingData($id, $user);
|
||||
if (null === $freshBookingData || $freshBookingData instanceof Notification) {
|
||||
$this->addFlash('error', 'Buchungsdaten konnten vor dem Speichern nicht neu geladen werden');
|
||||
$this->logger->warning('Failed to refresh booking before update submission', [
|
||||
'email' => $email,
|
||||
'booking_id' => $id,
|
||||
'has_notification' => $freshBookingData instanceof Notification,
|
||||
]);
|
||||
|
||||
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
|
||||
}
|
||||
|
||||
$bookingDto->booking = $freshBookingData;
|
||||
|
||||
$mutableData = $this->travelDataService->getMutabilityData(
|
||||
$freshBookingData->dateId,
|
||||
forceRefresh: true
|
||||
);
|
||||
if (null !== $mutableData) {
|
||||
$this->travelDataService->patchMutability($bookingDto->travel, $mutableData);
|
||||
}
|
||||
|
||||
$immutableChangesReverted = $this->submitGuard->reconcileImmutableCategories($bookingDto, $freshBookingData);
|
||||
if (true === $immutableChangesReverted) {
|
||||
$this->bookingSessionService->saveBookingDto($request, $bookingDto, BookingDto::MODE_EDIT);
|
||||
$this->addFlash('info', 'Einige Änderungen wurden verworfen, da sie aktuell nicht mehr änderbar sind.');
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->apiClient->updateBooking($bookingDto, true);
|
||||
if ($response instanceof Notification) {
|
||||
if (true === $response->isError()) {
|
||||
$this->addFlash('error', $response->message);
|
||||
} else {
|
||||
$this->addFlash('info', $response->message);
|
||||
}
|
||||
$this->logger->error('Booking update not successful', [
|
||||
'email' => $email,
|
||||
'booking_id' => $id,
|
||||
'message' => $response->message,
|
||||
]);
|
||||
} elseif (true === $response->success) {
|
||||
// Invalidate cache and clear session on success
|
||||
$this->dataLoader->invalidateBookingCache($id, $user);
|
||||
$this->bookingSessionService->clearBookingDto($request, BookingDto::MODE_EDIT);
|
||||
|
||||
// Delete draft on successful submission
|
||||
$this->draftService->deleteDraft($user, $id);
|
||||
|
||||
$this->addFlash('success', 'Buchung erfolgreich aktualisiert');
|
||||
$this->logger->info('Booking update successful', [
|
||||
'email' => $email,
|
||||
'booking_id' => $id,
|
||||
]);
|
||||
|
||||
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
|
||||
} else {
|
||||
// BookingUpdate received but success is false
|
||||
$this->addFlash('error', $response->status ?? 'Buchung konnte nicht aktualisiert werden');
|
||||
$this->logger->warning('Booking update returned unsuccessful status', [
|
||||
'email' => $email,
|
||||
'booking_id' => $id,
|
||||
'status' => $response->status,
|
||||
'valid' => $response->valid,
|
||||
]);
|
||||
}
|
||||
} catch (TimeoutException $e) {
|
||||
$this->addFlash('error', 'Die Anfrage hat zu lange gedauert. Bitte versuchen Sie es erneut.');
|
||||
$this->logger->error('Booking update timeout', [
|
||||
'email' => $email,
|
||||
'booking_id' => $id,
|
||||
'exception' => $e->getMessage(),
|
||||
]);
|
||||
} catch (ApiClientException) {
|
||||
$this->addFlash('error', 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten');
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user