137 lines
5.1 KiB
PHP
137 lines
5.1 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 App\Model\BookingEditSubmissionResult;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
/**
|
|
* 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 LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
public function handleSubmission(Request $request, BookingDto $bookingDto, int $bookingId, User $user): BookingEditSubmissionResult
|
|
{
|
|
$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->logger->warning('Failed to refresh booking before update submission', [
|
|
'email' => $email,
|
|
'booking_id' => $bookingId,
|
|
'has_notification' => $freshBookingData instanceof Notification,
|
|
]);
|
|
|
|
return new BookingEditSubmissionResult(
|
|
BookingEditSubmissionResult::STATUS_BOOKING_DATA_RELOAD_FAILED,
|
|
);
|
|
}
|
|
|
|
$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);
|
|
}
|
|
|
|
try {
|
|
$response = $this->apiClient->updateBooking($bookingDto, true);
|
|
if ($response instanceof Notification) {
|
|
$this->logger->error('Booking update not successful', [
|
|
'email' => $email,
|
|
'booking_id' => $bookingId,
|
|
'message' => $response->message,
|
|
]);
|
|
|
|
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->logger->info('Booking update successful', [
|
|
'email' => $email,
|
|
'booking_id' => $bookingId,
|
|
]);
|
|
|
|
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->logger->error('Booking update timeout', [
|
|
'email' => $email,
|
|
'booking_id' => $bookingId,
|
|
]);
|
|
|
|
return new BookingEditSubmissionResult(
|
|
BookingEditSubmissionResult::STATUS_TIMEOUT,
|
|
null,
|
|
$immutableChangesReverted,
|
|
);
|
|
} catch (ApiClientException) {
|
|
return new BookingEditSubmissionResult(
|
|
BookingEditSubmissionResult::STATUS_API_CLIENT_ERROR,
|
|
null,
|
|
$immutableChangesReverted,
|
|
);
|
|
}
|
|
}
|
|
}
|