getBookingDto($request, BookingDto::MODE_CREATE); if (null !== $bookingDto) { return $bookingDto; } throw new BookingSessionNotFoundException(); } /** * Gets or creates the baseline room selection snapshot for change detection. */ public function getOrCreateBaselineSnapshot(Request $request, BookingDto $bookingCreateDto): array { $baselineKey = self::BOOKING_CREATE_BASELINE_KEY; if (!$request->getSession()->has($baselineKey) || $request->query->has('reset_baseline')) { $baseline = $bookingCreateDto->createRoomSelectionSnapshot(); $request->getSession()->set($baselineKey, $baseline); return $baseline; } return $request->getSession()->get($baselineKey); } /** * Clears the baseline snapshot from the session. */ public function clearBaselineSnapshot(Request $request): void { $request->getSession()->remove(self::BOOKING_CREATE_BASELINE_KEY); } /** * Saves the booking DTO to the session. */ public function saveBookingDto(Request $request, BookingDto $bookingDto, string $mode): void { $sessionKey = $this->getSessionKey($mode); $request->getSession()->set($sessionKey, $bookingDto); } /** * Retrieves the booking DTO from the session and restores the Travel object. */ public function getBookingDto(Request $request, string $mode): ?BookingDto { $sessionKey = $this->getSessionKey($mode); $session = $request->getSession(); if (false === $session->has($sessionKey)) { return null; } $bookingDto = $session->get($sessionKey); $this->hydrate($bookingDto); return $bookingDto; } /** * Clears the booking DTO from the session. */ public function clearBookingDto(Request $request, string $mode): void { $sessionKey = $this->getSessionKey($mode); $request->getSession()->remove($sessionKey); } /** * Clears all booking-related session data except the return URL. */ public function clearBookingSession(Request $request): void { $session = $request->getSession(); $session->remove(self::BOOKING_CREATE_KEY); $session->remove(self::BOOKING_CREATE_BASELINE_KEY); } /** * Stores the return URL in the session. */ public function storeReturnUrl(Request $request, ?string $returnUrl): void { $url = self::DEFAULT_RETURN_URL; if (null !== $returnUrl && '' !== trim($returnUrl)) { if (false !== filter_var($returnUrl, \FILTER_VALIDATE_URL) && 1 === preg_match('#^https?://#i', $returnUrl)) { $url = $returnUrl; } } $request->getSession()->set(self::RETURN_URL_KEY, $url); } /** * Retrieves the return URL from the session. */ public function getReturnUrl(Request $request): string { return $request->getSession()->get(self::RETURN_URL_KEY, self::DEFAULT_RETURN_URL); } private function getSessionKey(string $mode): string { return BookingDto::MODE_EDIT === $mode ? self::BOOKING_EDIT_KEY : self::BOOKING_CREATE_KEY; } /** * Restores the full Travel object after session deserialization. */ 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; } } }