feat: reduce session payload

This commit is contained in:
Björn Fromme committed 2026-03-16 12:03:00 +01:00
1 parent a1518c06fb
commit 6fd5a218a3
2 files changed
+97 -8

No files matched your search

+39 -8
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\BusProNet\Constants;
@@ -80,14 +82,12 @@ class BookingService
*/
public function getOrCreateBookingCreateDto(Request $request): BookingDto
{
$bookingCreateDto = $request->getSession()->get(self::BOOKING_CREATE_KEY);
$bookingDto = $this->getBookingDto($request, BookingDto::MODE_CREATE);
// Return existing DTO from session if available
if (null !== $bookingCreateDto) {
return $bookingCreateDto;
if (null !== $bookingDto) {
return $bookingDto;
}
// No session found - user must go through proper init flow
throw new BookingSessionNotFoundException();
}
@@ -105,12 +105,15 @@ class BookingService
}
/**
* Retrieves the booking DTO from the session.
* Retrieves the booking DTO from the session and restores the Travel object.
*
* After deserialization the DTO contains only a Travel skeleton with the ID.
* This method replaces it with the full Travel from cache via hydrate().
*
* @param Request $request The HTTP request containing session data
* @param string $mode The booking mode (create/edit)
*
* @return BookingDto|null The booking DTO from session or null if not found
* @return BookingDto|null The booking DTO or null if not found
*/
public function getBookingDto(Request $request, string $mode): ?BookingDto
{
@@ -121,7 +124,10 @@ class BookingService
return null;
}
return $session->get($sessionKey);
$bookingDto = $session->get($sessionKey);
$this->hydrate($bookingDto);
return $bookingDto;
}
/**
@@ -148,6 +154,31 @@ class BookingService
return BookingDto::MODE_EDIT === $mode ? self::BOOKING_EDIT_KEY : self::BOOKING_CREATE_KEY;
}
/**
* Restores the full Travel object from cache after session deserialization.
*
* BookingDto::__serialize() replaces Travel with just its ID to keep session
* payloads small. This method fetches the complete Travel from the
* TravelDataService cache and sets it on both the DTO and the Booking reference.
*/
private function hydrate(BookingDto $bookingDto): void
{
$travel = $this->travelDataService->getTravelData(
$bookingDto->travel->id,
$bookingDto->hotelId
);
if (null === $travel) {
return;
}
$bookingDto->travel = $travel;
if (null !== $bookingDto->booking) {
$bookingDto->booking->travelData = $travel;
}
}
/**
* Clears all booking-related session data.
*