feat: reduce session payload
This commit is contained in:
@@ -325,4 +325,62 @@ class BookingDto
|
||||
{
|
||||
return AgencyLoader::INTERNAL_AGENCY_CODE === $this->agencyCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls session serialization to exclude the heavy Travel object graph.
|
||||
*
|
||||
* Replaces the full Travel object with just its integer ID. The Booking's
|
||||
* travelData reference is also removed since it points to the same object.
|
||||
* BookingService::hydrate() restores the Travel from cache after session read.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function __serialize(): array
|
||||
{
|
||||
$data = get_object_vars($this);
|
||||
|
||||
// Replace the full Travel object graph with just its ID
|
||||
$data['travel'] = $this->travel->id;
|
||||
|
||||
// Clone Booking to avoid mutating the live object, then strip its
|
||||
// travelData reference which points to the same heavy Travel graph
|
||||
if (null !== $this->booking) {
|
||||
$booking = clone $this->booking;
|
||||
$booking->travelData = null;
|
||||
$data['booking'] = $booking;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the DTO from session data with a minimal Travel placeholder.
|
||||
*
|
||||
* Creates a Travel object containing only the ID. BookingService::hydrate()
|
||||
* replaces this with the full Travel from cache on every session read.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function __unserialize(array $data): void
|
||||
{
|
||||
// Extract the travel ID before the property loop — 'travel' in the
|
||||
// serialized data is an int, not a Travel object
|
||||
$travelId = $data['travel'];
|
||||
unset($data['travel']);
|
||||
|
||||
// Skip keys that no longer exist as declared properties to avoid
|
||||
// dynamic property creation (deprecated since PHP 8.2)
|
||||
foreach ($data as $key => $value) {
|
||||
if (false === property_exists($this, $key)) {
|
||||
continue;
|
||||
}
|
||||
$this->$key = $value;
|
||||
}
|
||||
|
||||
// Create a skeleton Travel with only the ID; BookingService::hydrate()
|
||||
// replaces this with the full object from cache
|
||||
$travel = new Travel();
|
||||
$travel->id = $travelId;
|
||||
$this->travel = $travel;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user