130 lines
5.3 KiB
PHP
130 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Exception\ApiClientException;
|
|
use App\BusProNet\Exception\TimeoutException;
|
|
use App\BusProNet\Model\Notification;
|
|
use App\Entity\User;
|
|
use App\Form\Model\BookingDto;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
/**
|
|
* Handles the final booking edit submission workflow.
|
|
*/
|
|
class BookingEditSubmitter
|
|
{
|
|
public function __construct(
|
|
private readonly ApiClient $apiClient,
|
|
private readonly BookingEditDataLoader $dataLoader,
|
|
private readonly BookingEditDraftManager $draftService,
|
|
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
|
|
{
|
|
$email = $user->getEmail();
|
|
|
|
$this->logger->info('Initiated booking update', [
|
|
'email' => $email,
|
|
'booking_id' => $bookingId,
|
|
]);
|
|
|
|
$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);
|
|
}
|
|
|
|
$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($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) {
|
|
$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,
|
|
]);
|
|
}
|
|
} 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,
|
|
]);
|
|
} catch (ApiClientException) {
|
|
$this->addFlash($request, 'error', 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten');
|
|
}
|
|
|
|
return $this->redirectToEdit($bookingId);
|
|
}
|
|
|
|
private function addFlash(Request $request, string $type, string $message): void
|
|
{
|
|
$request->getSession()->getFlashBag()->add($type, $message);
|
|
}
|
|
|
|
private function redirectToEdit(int $bookingId): RedirectResponse
|
|
{
|
|
return new RedirectResponse($this->urlGenerator->generate('app_booking_edit', ['id' => $bookingId]));
|
|
}
|
|
}
|