feat: move scope of enforced 'option' status to full booking

addresses #869chtfeq
This commit is contained in:
Björn Fromme
2026-03-19 16:15:07 +01:00
parent 45d05786da
commit df8566e1b5
19 changed files with 388 additions and 329 deletions
@@ -6,7 +6,6 @@ namespace App\BusProNet\DataProcessor;
use App\BusProNet\Constants;
use App\BusProNet\Model\Booking;
use App\BusProNet\Service\ParticipantStatusRuleRegistry;
use App\Form\Model\BookingDto;
/**
@@ -17,9 +16,10 @@ use App\Form\Model\BookingDto;
*/
class BookingPayloadBuilder
{
private const DEFAULT_PARTICIPANT_STATUS = 'F';
public function __construct(
private readonly ServiceMappingCollector $mappingCollector,
private readonly ParticipantStatusRuleRegistry $statusRuleRegistry,
) {
}
@@ -302,7 +302,7 @@ class BookingPayloadBuilder
foreach ($bookingDto->participants as $index => $participant) {
$participantData = [
'@id' => $index + 1,
'status' => $this->statusRuleRegistry->evaluateStatus($participant),
'status' => self::DEFAULT_PARTICIPANT_STATUS,
'name' => $participant->lastName,
'vorname' => $participant->firstName,
'geschlecht' => $participant->gender ?? '',
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\BusProNet\Service;
use App\BusProNet\Service\Contract\BookingStatusRuleInterface;
use App\Form\Model\BookingDto;
/**
* Registry for booking status evaluation rules.
*
* Evaluates registered rules in priority order and returns the first matching
* status. Falls back to 'F' when no rule applies.
*/
class BookingStatusRuleRegistry
{
private const DEFAULT_STATUS = 'F';
/**
* @var BookingStatusRuleInterface[]
*/
private array $sortedRules;
/**
* @param BookingStatusRuleInterface[] $rules
*/
public function __construct(array $rules)
{
$this->sortedRules = $this->sortRulesByPriority($rules);
}
public function evaluateStatus(BookingDto $bookingDto): string
{
foreach ($this->sortedRules as $rule) {
if (true === $rule->evaluate($bookingDto)) {
return $rule->getStatus();
}
}
return self::DEFAULT_STATUS;
}
/**
* @param BookingStatusRuleInterface[] $rules
*
* @return BookingStatusRuleInterface[]
*/
private function sortRulesByPriority(array $rules): array
{
usort($rules, static fn (
BookingStatusRuleInterface $a,
BookingStatusRuleInterface $b,
): int => $b->getPriority() <=> $a->getPriority());
return $rules;
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\BusProNet\Service\Contract;
use App\Form\Model\BookingDto;
/**
* Defines the contract for booking status evaluation rules.
*
* Rules are evaluated in priority order. The first matching rule determines
* the enforced booking status.
*/
interface BookingStatusRuleInterface
{
/**
* Evaluates whether this rule applies to the given booking.
*/
public function evaluate(BookingDto $bookingDto): bool;
/**
* Returns the status code to assign when this rule matches.
*/
public function getStatus(): string;
/**
* Returns the rule priority (higher values evaluated first).
*/
public function getPriority(): int;
/**
* Returns a human-readable rule description.
*/
public function getDescription(): string;
}
@@ -1,52 +0,0 @@
<?php
declare(strict_types=1);
namespace App\BusProNet\Service\Contract;
use App\Form\Model\ParticipantDto;
/**
* Defines the contract for participant status evaluation rules.
*
* Status rules are evaluated in priority order to determine the appropriate
* status code for a participant during booking creation. The first rule that
* matches (evaluate returns true) determines the participant's status.
*/
interface ParticipantStatusRuleInterface
{
/**
* Evaluates whether this rule applies to the given participant.
*
* @param ParticipantDto $participant The participant to evaluate
*
* @return bool True if this rule applies, false otherwise
*/
public function evaluate(ParticipantDto $participant): bool;
/**
* Returns the status code to assign when this rule matches.
*
* @return string The status code (e.g., 'O' for Option, 'F' for Final)
*/
public function getStatus(): string;
/**
* Returns the priority of this rule.
*
* Higher priority rules are evaluated first. Rules with the same priority
* are evaluated in registration order.
*
* @return int The priority value (higher = evaluated first)
*/
public function getPriority(): int;
/**
* Returns a human-readable description of this rule.
*
* Used for debugging and logging purposes.
*
* @return string Description of what this rule checks
*/
public function getDescription(): string;
}
@@ -1,71 +0,0 @@
<?php
declare(strict_types=1);
namespace App\BusProNet\Service;
use App\BusProNet\Service\Contract\ParticipantStatusRuleInterface;
use App\Form\Model\ParticipantDto;
/**
* Registry for participant status evaluation rules.
*
* Manages a collection of status rules and evaluates them in priority order
* to determine the appropriate status code for a participant during booking
* creation. Returns a default status of 'F' (Final) when no rules match.
*/
class ParticipantStatusRuleRegistry
{
private const DEFAULT_STATUS = 'F';
/**
* @var ParticipantStatusRuleInterface[]
*/
private array $sortedRules;
/**
* @param ParticipantStatusRuleInterface[] $rules The rules to register
*/
public function __construct(array $rules)
{
$this->sortedRules = $this->sortRulesByPriority($rules);
}
/**
* Evaluates all rules to determine the participant's status.
*
* Rules are evaluated in priority order (highest first). The first rule
* that matches determines the status. Returns 'F' if no rules match.
*
* @param ParticipantDto $participant The participant to evaluate
*
* @return string The determined status code
*/
public function evaluateStatus(ParticipantDto $participant): string
{
foreach ($this->sortedRules as $rule) {
if (true === $rule->evaluate($participant)) {
return $rule->getStatus();
}
}
return self::DEFAULT_STATUS;
}
/**
* Sorts rules by priority in descending order.
*
* @param ParticipantStatusRuleInterface[] $rules The rules to sort
*
* @return ParticipantStatusRuleInterface[] The sorted rules
*/
private function sortRulesByPriority(array $rules): array
{
usort($rules, static fn (
ParticipantStatusRuleInterface $a,
ParticipantStatusRuleInterface $b,
): int => $b->getPriority() <=> $a->getPriority());
return $rules;
}
}
@@ -5,26 +5,37 @@ declare(strict_types=1);
namespace App\BusProNet\Service\StatusRule;
use App\BusProNet\Model\Service;
use App\BusProNet\Service\Contract\ParticipantStatusRuleInterface;
use App\BusProNet\Service\Contract\BookingStatusRuleInterface;
use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
/**
* Status rule for participants with chaperon (Begleitperson) services.
* Status rule for bookings with chaperon (Begleitperson) services.
*
* Assigns 'Option' status to participants who have selected any service
* containing 'Begleitperson' in its label. This applies to accompanying
* persons whose booking confirmation may depend on the main participant.
* Assigns 'Option' status to bookings where any participant has selected any
* service containing 'Begleitperson' in its label.
*/
class ChaperonServiceStatusRule implements ParticipantStatusRuleInterface
class ChaperonServiceStatusRule implements BookingStatusRuleInterface
{
private const STATUS = 'O';
private const PRIORITY = 100;
private const SEARCH_TERM = 'begleitperson';
/**
* @see ParticipantStatusRuleInterface::evaluate()
* @see BookingStatusRuleInterface::evaluate()
*/
public function evaluate(ParticipantDto $participant): bool
public function evaluate(BookingDto $bookingDto): bool
{
foreach ($bookingDto->participants as $participant) {
if (true === $this->participantHasChaperonService($participant)) {
return true;
}
}
return false;
}
private function participantHasChaperonService(ParticipantDto $participant): bool
{
// Check array services
$arrayServices = [
@@ -58,7 +69,7 @@ class ChaperonServiceStatusRule implements ParticipantStatusRuleInterface
}
/**
* @see ParticipantStatusRuleInterface::getStatus()
* @see BookingStatusRuleInterface::getStatus()
*/
public function getStatus(): string
{
@@ -66,7 +77,7 @@ class ChaperonServiceStatusRule implements ParticipantStatusRuleInterface
}
/**
* @see ParticipantStatusRuleInterface::getPriority()
* @see BookingStatusRuleInterface::getPriority()
*/
public function getPriority(): int
{
@@ -74,7 +85,7 @@ class ChaperonServiceStatusRule implements ParticipantStatusRuleInterface
}
/**
* @see ParticipantStatusRuleInterface::getDescription()
* @see BookingStatusRuleInterface::getDescription()
*/
public function getDescription(): string
{
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Controller\Booking\Create;
use App\BusProNet\Constants;
use App\Controller\Booking\Traits\BookingCreateTrait;
use App\Controller\Booking\Traits\BookingExceptionHandlerTrait;
use App\Controller\Booking\Traits\ParticipantCardFlowTrait;
@@ -86,6 +85,7 @@ class Step2Controller extends AbstractController
// Preselect mandatory services
$this->bookingService->preselectDefaultServices($bookingCreateDto);
$this->bookingService->applyCreateBookingStatusRules($bookingCreateDto);
// Save BookingDto to session
$this->bookingService->saveBookingDto($request, $bookingCreateDto, BookingDto::MODE_CREATE);
@@ -162,8 +162,7 @@ class Step2Controller extends AbstractController
// Keep behavior consistent with cards view: newly filled participant data
// (especially dateOfBirth) must immediately trigger mandatory service preselection.
$this->bookingService->preselectDefaultServices($bookingDto);
$bookingDto->bookingStatus = Constants::BOOKING_STATUS_OPEN;
$this->bookingService->applyCreateBookingStatusRules($bookingDto);
$this->bookingService->saveBookingDto($request, $bookingDto, BookingDto::MODE_CREATE);
// Recreate form with filled DTO so the view shows the dummy data
@@ -176,6 +175,7 @@ class Step2Controller extends AbstractController
// Re-run default preselection after participant form input changes (e.g. DOB).
// This ensures auto-book defaults are applied as soon as eligibility becomes known.
$this->bookingService->preselectDefaultServices($bookingDto);
$this->bookingService->applyCreateBookingStatusRules($bookingDto);
}
// Collect notifications from field handlers (run during PRE_SUBMIT)
@@ -70,6 +70,8 @@ class Step3Controller extends AbstractController
}
try {
$this->bookingService->applyCreateBookingStatusRules($bookingCreateDto);
// Validate booking data with API by submitting an inquiry booking
$inquiryResponse = $this->apiClient->createBookingInquiry($bookingCreateDto);
@@ -86,6 +86,8 @@ class Step4Controller extends AbstractController
if (true === $form->isSubmitted() && true === $form->isValid()) {
try {
$this->bookingService->applyCreateBookingStatusRules($bookingCreateDto);
// Submit final booking (already validated in Step 3)
$bookingResponse = $this->apiClient->createBooking($bookingCreateDto);
@@ -131,6 +131,9 @@ trait ParticipantCardFlowTrait
// Refresh endpoint is POST-only and always processes submitted participant data.
$this->bookingService->preselectDefaultServices($bookingDto);
if (BookingDto::MODE_CREATE === $bookingDto->getMode()) {
$this->bookingService->applyCreateBookingStatusRules($bookingDto);
}
// Recreate form so the rendered state reflects any new auto-preselections.
$form = $this->createParticipantForm($bookingDto, $index, $refreshFormOptions);
+1 -1
View File
@@ -54,7 +54,7 @@ class BookingDto
/**
* Booking status code for API submission.
* Values: 'F' (Final/Frei), 'A' (Anfrage/Inquiry).
* Values: 'F' (Fest), 'O' (Option), 'A' (Anfrage/Inquiry).
*/
public string $bookingStatus = 'F';
+16
View File
@@ -8,6 +8,7 @@ use App\BusProNet\Constants;
use App\BusProNet\Model\Room;
use App\BusProNet\Model\Service;
use App\BusProNet\Model\Travel;
use App\BusProNet\Service\BookingStatusRuleRegistry;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Exception\BookingSessionNotFoundException;
use App\Exception\NoRoomsAvailableException;
@@ -31,6 +32,7 @@ class BookingService
private readonly TravelDataService $travelDataService,
private readonly BookingPriceCalculatorService $priceCalculator,
private readonly ParticipantEligibilityService $participantEligibilityService,
private readonly BookingStatusRuleRegistry $bookingStatusRuleRegistry,
private readonly AgencyLoader $agencyLoader,
#[Autowire('%default_booking_status%')]
private readonly string $defaultBookingStatus,
@@ -982,6 +984,20 @@ class BookingService
}
}
/**
* Applies booking-level status rules in create flow.
*
* Inquiry bookings always win and are never overridden.
*/
public function applyCreateBookingStatusRules(BookingDto $bookingDto): void
{
if (Constants::BOOKING_STATUS_INQUIRY === $bookingDto->bookingStatus) {
return;
}
$bookingDto->bookingStatus = $this->bookingStatusRuleRegistry->evaluateStatus($bookingDto);
}
/**
* Ensures the booking DTO has the correct number of participant objects.
*