chore: further cleanup and refactor

This commit is contained in:
Björn Fromme
2026-04-19 15:45:02 +02:00
parent 5a0bf11c81
commit 80969d1da0
21 changed files with 606 additions and 321 deletions
+5 -5
View File
@@ -27,7 +27,7 @@ class BookingEditDataLoader
{
public const string CACHE_TAG_USER_PREFIX = 'user_bookings_';
private bool $draftWasRestored = false;
private bool $draftRestored = false;
public function __construct(
private readonly ApiClient $apiClient,
@@ -48,9 +48,9 @@ class BookingEditDataLoader
* This flag is reset on each call to loadFormData() and can be used
* by the controller to show a flash message to the user.
*/
public function wasDraftRestored(): bool
public function isDraftRestored(): bool
{
return $this->draftWasRestored;
return $this->draftRestored;
}
/**
@@ -71,7 +71,7 @@ class BookingEditDataLoader
*/
public function loadFormData(Request $request, int $bookingId, User $user): ?BookingDto
{
$this->draftWasRestored = false;
$this->draftRestored = false;
$formData = $this->bookingSessionService->getBookingDto($request, BookingDto::MODE_EDIT);
@@ -154,7 +154,7 @@ class BookingEditDataLoader
if (null !== $draft) {
$applied = $this->draftService->applyDraftToDto($draft, $formData, $travelData);
if (true === $applied) {
$this->draftWasRestored = true;
$this->draftRestored = true;
}
}
+43 -42
View File
@@ -9,12 +9,10 @@ use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\TimeoutException;
use App\BusProNet\Model\Notification;
use App\Entity\User;
use App\Model\BookingEditSubmissionResult;
use App\Form\Model\BookingDto;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Handles the final booking edit submission workflow.
@@ -28,12 +26,11 @@ class BookingEditSubmitter
private readonly TravelDataProvider $travelDataService,
private readonly BookingEditSubmitGuard $submitGuard,
private readonly BookingSessionManager $bookingSessionService,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly LoggerInterface $logger,
) {
}
public function handleSubmission(Request $request, BookingDto $bookingDto, int $bookingId, User $user): RedirectResponse
public function handleSubmission(Request $request, BookingDto $bookingDto, int $bookingId, User $user): BookingEditSubmissionResult
{
$email = $user->getEmail();
@@ -45,14 +42,15 @@ class BookingEditSubmitter
$this->dataLoader->invalidateBookingCache($bookingId, $user);
$freshBookingData = $this->dataLoader->fetchBookingData($bookingId, $user);
if (null === $freshBookingData || $freshBookingData instanceof Notification) {
$this->addFlash($request, 'error', 'Buchungsdaten konnten vor dem Speichern nicht neu geladen werden');
$this->logger->warning('Failed to refresh booking before update submission', [
'email' => $email,
'booking_id' => $bookingId,
'has_notification' => $freshBookingData instanceof Notification,
]);
return $this->redirectToEdit($bookingId);
return new BookingEditSubmissionResult(
BookingEditSubmissionResult::STATUS_BOOKING_DATA_RELOAD_FAILED,
);
}
$bookingDto->booking = $freshBookingData;
@@ -68,66 +66,69 @@ class BookingEditSubmitter
$immutableChangesReverted = $this->submitGuard->reconcileImmutableCategories($bookingDto, $freshBookingData);
if (true === $immutableChangesReverted) {
$this->bookingSessionService->saveBookingDto($request, $bookingDto, BookingDto::MODE_EDIT);
$this->addFlash($request, '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($request, 'error', $response->message);
} else {
$this->addFlash($request, 'info', $response->message);
}
$this->logger->error('Booking update not successful', [
'email' => $email,
'booking_id' => $bookingId,
'message' => $response->message,
]);
} elseif (true === $response->success) {
return new BookingEditSubmissionResult(
true === $response->isError()
? BookingEditSubmissionResult::STATUS_NOTIFICATION_ERROR
: BookingEditSubmissionResult::STATUS_NOTIFICATION_INFO,
$response->message,
$immutableChangesReverted,
);
}
if (true === $response->success) {
$this->dataLoader->invalidateBookingCache($bookingId, $user);
$this->bookingSessionService->clearBookingDto($request, BookingDto::MODE_EDIT);
$this->draftService->deleteDraft($user, $bookingId);
$this->addFlash($request, 'success', 'Buchung erfolgreich aktualisiert');
$this->logger->info('Booking update successful', [
'email' => $email,
'booking_id' => $bookingId,
]);
return $this->redirectToEdit($bookingId);
} else {
$this->addFlash($request, 'error', $response->status ?? 'Buchung konnte nicht aktualisiert werden');
$this->logger->warning('Booking update returned unsuccessful status', [
'email' => $email,
'booking_id' => $bookingId,
'status' => $response->status,
'valid' => $response->valid,
]);
return new BookingEditSubmissionResult(
BookingEditSubmissionResult::STATUS_SUCCESS,
null,
$immutableChangesReverted,
);
}
$this->logger->warning('Booking update returned unsuccessful status', [
'email' => $email,
'booking_id' => $bookingId,
'status' => $response->status,
'valid' => $response->valid,
]);
return new BookingEditSubmissionResult(
BookingEditSubmissionResult::STATUS_UNSUCCESSFUL,
$response->status ?? 'Buchung konnte nicht aktualisiert werden',
$immutableChangesReverted,
);
} catch (TimeoutException) {
$this->addFlash($request, 'error', 'Die Anfrage hat zu lange gedauert. Bitte versuchen Sie es erneut.');
$this->logger->error('Booking update timeout', [
'email' => $email,
'booking_id' => $bookingId,
]);
return new BookingEditSubmissionResult(
BookingEditSubmissionResult::STATUS_TIMEOUT,
null,
$immutableChangesReverted,
);
} catch (ApiClientException) {
$this->addFlash($request, 'error', 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten');
return new BookingEditSubmissionResult(
BookingEditSubmissionResult::STATUS_API_CLIENT_ERROR,
null,
$immutableChangesReverted,
);
}
return $this->redirectToEdit($bookingId);
}
private function addFlash(Request $request, string $type, string $message): void
{
$session = $request->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add($type, $message);
}
}
private function redirectToEdit(int $bookingId): RedirectResponse
{
return new RedirectResponse($this->urlGenerator->generate('app_booking_edit', ['id' => $bookingId]));
}
}