wip: refactor to cards with individual participant forms
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Form\Model\BookingDto;
|
||||
|
||||
/**
|
||||
* Extracts card display data for participants in card-based booking flows.
|
||||
*
|
||||
* Provides participant name, room assignment, and individual pricing for
|
||||
* display in the card overview UI.
|
||||
*/
|
||||
class ParticipantCardDataService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BookingPriceCalculatorService $priceCalculator,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get card data for a single participant.
|
||||
*
|
||||
* @return array{name: string, roomName: string, price: string}
|
||||
*/
|
||||
public function getCardData(BookingDto $bookingDto, int $index): array
|
||||
{
|
||||
$participant = $bookingDto->participants[$index] ?? null;
|
||||
|
||||
if (null === $participant) {
|
||||
throw new \InvalidArgumentException(sprintf('Participant at index %d does not exist', $index));
|
||||
}
|
||||
|
||||
// Extract participant name with fallback
|
||||
$name = $this->getParticipantName($participant, $index);
|
||||
|
||||
// Extract room name
|
||||
$roomName = $this->getRoomName($bookingDto, $participant);
|
||||
|
||||
// Calculate and format individual price
|
||||
$price = $this->getFormattedPrice($bookingDto, $index);
|
||||
|
||||
return [
|
||||
'name' => $name,
|
||||
'roomName' => $roomName,
|
||||
'price' => $price,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get card data for all participants.
|
||||
*
|
||||
* @return array<int, array{name: string, roomName: string, price: string}>
|
||||
*/
|
||||
public function getAllCardsData(BookingDto $bookingDto): array
|
||||
{
|
||||
$cardsData = [];
|
||||
|
||||
foreach ($bookingDto->participants as $index => $participant) {
|
||||
$cardsData[$index] = $this->getCardData($bookingDto, $index);
|
||||
}
|
||||
|
||||
return $cardsData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get participant name with fallback to generic label.
|
||||
*/
|
||||
private function getParticipantName(object $participant, int $index): string
|
||||
{
|
||||
$firstName = $participant->firstName ?? '';
|
||||
$lastName = $participant->lastName ?? '';
|
||||
|
||||
$name = trim($firstName . ' ' . $lastName);
|
||||
|
||||
if ('' === $name) {
|
||||
return sprintf('Teilnehmer %d', $index + 1);
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get room name from travel model.
|
||||
*/
|
||||
private function getRoomName(BookingDto $bookingDto, object $participant): string
|
||||
{
|
||||
$roomId = $participant->assignedRoomId ?? null;
|
||||
|
||||
if (null === $roomId) {
|
||||
return 'Kein Zimmer zugewiesen';
|
||||
}
|
||||
|
||||
$room = $bookingDto->travel->getRoomById($roomId);
|
||||
|
||||
if (null === $room) {
|
||||
return 'Unbekanntes Zimmer';
|
||||
}
|
||||
|
||||
return $room->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate and format individual participant price.
|
||||
*/
|
||||
private function getFormattedPrice(BookingDto $bookingDto, int $index): string
|
||||
{
|
||||
$prices = $this->priceCalculator->calculateAllParticipantIndividualPrices($bookingDto);
|
||||
|
||||
$price = $prices[$index] ?? 0.0;
|
||||
|
||||
return number_format($price, 2, ',', '.') . ' €';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user