feat: centralize create/edit flow contexts, extract edit submit service
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Form\Model\BookingCreateContext;
|
||||
use App\Form\Model\BookingDto;
|
||||
|
||||
/**
|
||||
* Prepares the shared view model for booking create.
|
||||
*/
|
||||
class BookingCreateContextFactory
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BookingRoomSelectionService $roomSelectionService,
|
||||
private readonly ParticipantCardDataService $participantCardDataService,
|
||||
private readonly BookingSummaryDataService $summaryDataService,
|
||||
private readonly BookingPriceCalculatorService $priceCalculator,
|
||||
) {
|
||||
}
|
||||
|
||||
public function create(
|
||||
BookingDto $bookingDto,
|
||||
string $pricingMode = RoomPricingCalculator::PRICING_MODE_ASSIGNMENT,
|
||||
): BookingCreateContext
|
||||
{
|
||||
$baseContext = $this->buildBaseContext($bookingDto, $pricingMode);
|
||||
|
||||
return new BookingCreateContext(
|
||||
bookingDto: $bookingDto,
|
||||
summaryData: $baseContext['summaryData'],
|
||||
groupedRooms: $baseContext['groupedRooms'],
|
||||
);
|
||||
}
|
||||
|
||||
public function createWithParticipantCards(BookingDto $bookingDto, bool $isSubmitted): BookingCreateContext
|
||||
{
|
||||
$baseContext = $this->buildBaseContext($bookingDto, RoomPricingCalculator::PRICING_MODE_SELECTION);
|
||||
|
||||
return new BookingCreateContext(
|
||||
bookingDto: $bookingDto,
|
||||
summaryData: $baseContext['summaryData'],
|
||||
groupedRooms: $baseContext['groupedRooms'],
|
||||
cardsData: $this->participantCardDataService->getAllCardsDataWithValidation($bookingDto),
|
||||
isSubmitted: $isSubmitted,
|
||||
);
|
||||
}
|
||||
|
||||
public function createWithParticipantPrices(BookingDto $bookingDto): BookingCreateContext
|
||||
{
|
||||
$baseContext = $this->buildBaseContext($bookingDto, RoomPricingCalculator::PRICING_MODE_ASSIGNMENT);
|
||||
|
||||
return new BookingCreateContext(
|
||||
bookingDto: $bookingDto,
|
||||
summaryData: $baseContext['summaryData'],
|
||||
groupedRooms: $baseContext['groupedRooms'],
|
||||
participantPrices: $this->priceCalculator->calculateAllParticipantIndividualPrices($bookingDto),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{summaryData: \App\Form\Model\BookingSummaryDto, groupedRooms: array{by_pax: array<int, \App\BusProNet\Model\Room>, by_room: array<int, \App\BusProNet\Model\Room>}}
|
||||
*/
|
||||
private function buildBaseContext(BookingDto $bookingDto, string $pricingMode): array
|
||||
{
|
||||
return [
|
||||
'summaryData' => $this->summaryDataService->getSummaryData($bookingDto, $pricingMode),
|
||||
'groupedRooms' => $this->roomSelectionService->groupRoomsBySelectionType(
|
||||
$bookingDto->travel->getAvailableRooms()
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\BookingEditContext;
|
||||
use App\Form\Model\BookingSummaryDto;
|
||||
|
||||
/**
|
||||
* Prepares the shared booking edit flow context.
|
||||
*/
|
||||
class BookingEditContextFactory
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ParticipantCardDataService $participantCardDataService,
|
||||
private readonly BookingSummaryDataService $summaryDataService,
|
||||
private readonly TravelDataService $travelDataService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function prepareBookingDto(BookingDto $bookingDto): void
|
||||
{
|
||||
$this->travelDataService->enrichWithFreshAvailabilities($bookingDto->travel);
|
||||
}
|
||||
|
||||
private function createSummaryData(BookingDto $bookingDto): BookingSummaryDto
|
||||
{
|
||||
// Shared by the overview and participant contexts, including refresh fallback.
|
||||
return $this->summaryDataService->getSummaryData($bookingDto);
|
||||
}
|
||||
|
||||
public function createParticipantContext(BookingDto $bookingDto, ?Booking $bookingData): BookingEditContext
|
||||
{
|
||||
return new BookingEditContext(
|
||||
bookingDto: $bookingDto,
|
||||
bookingData: $bookingData,
|
||||
mutableData: $bookingData ? $this->travelDataService->getMutabilityData($bookingData->dateId) : null,
|
||||
summaryData: $this->createSummaryData($bookingDto),
|
||||
);
|
||||
}
|
||||
|
||||
public function createOverviewContext(
|
||||
BookingDto $bookingDto,
|
||||
Booking $bookingData,
|
||||
bool $isDirty,
|
||||
bool $isSubmitted,
|
||||
bool $hasValidationErrors,
|
||||
): BookingEditContext {
|
||||
return new BookingEditContext(
|
||||
bookingDto: $bookingDto,
|
||||
bookingData: $bookingData,
|
||||
mutableData: $this->travelDataService->getMutabilityData($bookingData->dateId),
|
||||
summaryData: $this->createSummaryData($bookingDto),
|
||||
cardsData: $this->participantCardDataService->getAllCardsDataWithValidation($bookingDto),
|
||||
isDirty: $isDirty,
|
||||
isSubmitted: $isSubmitted,
|
||||
hasValidationErrors: $hasValidationErrors,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\BookingEditParticipantContext;
|
||||
|
||||
/**
|
||||
* Prepares the shared edit-participant page context.
|
||||
*/
|
||||
class BookingEditParticipantContextFactory
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BookingSummaryDataService $summaryDataService,
|
||||
private readonly TravelDataService $travelDataService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function prepareBookingDto(BookingDto $bookingDto): void
|
||||
{
|
||||
$this->travelDataService->enrichWithFreshAvailabilities($bookingDto->travel);
|
||||
}
|
||||
|
||||
public function create(BookingDto $bookingDto, Booking $bookingData): BookingEditParticipantContext
|
||||
{
|
||||
return new BookingEditParticipantContext(
|
||||
bookingDto: $bookingDto,
|
||||
bookingData: $bookingData,
|
||||
mutableData: $this->travelDataService->getMutabilityData($bookingData->dateId),
|
||||
summaryData: $this->summaryDataService->getSummaryData($bookingDto),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?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\Request;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
/**
|
||||
* Handles the final booking edit submission workflow.
|
||||
*/
|
||||
class BookingEditSubmitService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ApiClient $apiClient,
|
||||
private readonly BookingEditDataLoaderService $dataLoader,
|
||||
private readonly BookingEditDraftService $draftService,
|
||||
private readonly TravelDataService $travelDataService,
|
||||
private readonly BookingEditSubmitGuardService $submitGuard,
|
||||
private readonly BookingSessionService $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]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user