Files
myep/src/Service/ParticipantCardDataService.php
T

125 lines
3.5 KiB
PHP

<?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 0 === $index ? 'Anmelder:in' : sprintf('Teilnehmer:in %d', $index);
}
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->label ?? 'Unbekanntes Zimmer';
}
/**
* Calculate and format individual participant price.
*
* Returns a dash (-) when the price is zero and no room is assigned,
* indicating incomplete configuration rather than a zero-cost booking.
*/
private function getFormattedPrice(BookingDto $bookingDto, int $index): string
{
$prices = $this->priceCalculator->calculateAllParticipantIndividualPrices($bookingDto);
$price = $prices[$index] ?? 0.0;
// Display dash when price is zero and no room assigned (incomplete configuration)
$participant = $bookingDto->participants[$index] ?? null;
if (0.0 === $price && (null === $participant || null === $participant->assignedRoomId)) {
return '-';
}
return number_format($price, 2, ',', '.').' €';
}
}