wip: booking process
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BusProNet\Model\Room;
|
||||
use App\BusProNet\XmlLoader\HotelLoader;
|
||||
use App\BusProNet\XmlLoader\TravelLoader;
|
||||
use App\Form\Model\BookingCreateDto;
|
||||
use App\Form\Model\RoomSelectionDto;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class BookingService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TravelLoader $travelDataLoader,
|
||||
private readonly HotelLoader $hotelDataLoader,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getOrCreateBookingCreateDto(Request $request): BookingCreateDto
|
||||
{
|
||||
$bookingUuid = $request->query->get('uid');
|
||||
$bookingCreateDto = $request->getSession()->get('booking_create');
|
||||
|
||||
// No UID parameter - try to get existing DTO from session
|
||||
if (null === $bookingUuid) {
|
||||
return $bookingCreateDto ?? throw new NotFoundHttpException('No booking data found. Please start from the beginning.');
|
||||
}
|
||||
|
||||
// Create a new DTO - we need travel_id and hotel_id for this
|
||||
$travelId = $request->query->getInt('travel_id');
|
||||
$hotelId = $request->query->getInt('hotel_id');
|
||||
|
||||
if (0 === $travelId || 0 === $hotelId) {
|
||||
throw new NotFoundHttpException('Missing travel_id or hotel_id parameters');
|
||||
}
|
||||
|
||||
$travelData = $this->travelDataLoader->loadById($travelId, $hotelId);
|
||||
if (null === $travelData) {
|
||||
throw new NotFoundHttpException('Travel data not found');
|
||||
}
|
||||
|
||||
$hotelData = $this->hotelDataLoader->loadById($hotelId);
|
||||
if (null === $hotelData) {
|
||||
throw new NotFoundHttpException('Hotel data not found');
|
||||
}
|
||||
|
||||
$travelData->hotel = $hotelData;
|
||||
$roomsIdsAndQuantities = $this->processRoomQuantities($request);
|
||||
$availableRooms = $travelData->getAvailableRooms();
|
||||
|
||||
$roomSelections = array_map(
|
||||
fn (Room $room) => $this->createRoomSelection($room, $roomsIdsAndQuantities),
|
||||
$availableRooms
|
||||
);
|
||||
|
||||
$bookingCreateDto = new BookingCreateDto($travelData, $hotelId);
|
||||
$bookingCreateDto->roomSelections = $roomSelections;
|
||||
|
||||
$this->saveBookingCreateDto($request, $bookingCreateDto);
|
||||
|
||||
return $bookingCreateDto;
|
||||
}
|
||||
|
||||
public function saveBookingCreateDto(Request $request, BookingCreateDto $bookingCreateDto): void
|
||||
{
|
||||
$request->getSession()->set('booking_create', $bookingCreateDto);
|
||||
}
|
||||
|
||||
private function createRoomSelection(Room $room, array $roomsIdsAndQuantities): RoomSelectionDto
|
||||
{
|
||||
$selection = new RoomSelectionDto();
|
||||
$selection->roomId = $room->id;
|
||||
$selection->roomLabel = $room->label;
|
||||
$selection->maxQuantity = $room->available;
|
||||
$selection->minPax = $room->minPax;
|
||||
$selection->quantity = $roomsIdsAndQuantities[$room->id] ?? 0;
|
||||
|
||||
return $selection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts room IDs and quantities from the request data.
|
||||
*
|
||||
* Processes the 'rooms' parameter from the request to extract room IDs
|
||||
* as keys and their corresponding quantities as integer values. Filters
|
||||
* out empty values and converts all quantities to integers.
|
||||
*
|
||||
* @param Request $request The HTTP request containing room data
|
||||
*
|
||||
* @return array<int, int> Array with room IDs as keys and quantities as values
|
||||
*/
|
||||
public function processRoomQuantities(Request $request): array
|
||||
{
|
||||
$rooms = $request->request->all('rooms');
|
||||
|
||||
return array_map('intval', array_filter($rooms, 'strlen'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user