fix: enable bookings without ski passes
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Validates that a ski pass is selected whenever one is required.
|
||||
*
|
||||
* Applied on ParticipantEditDto because the decision needs the ParticipantEligibilityChecker,
|
||||
* which a DTO callback cannot reach.
|
||||
*/
|
||||
#[\Attribute]
|
||||
class SkiPassSelection extends Constraint
|
||||
{
|
||||
public string $message = 'Bitte auswählen';
|
||||
|
||||
public string $notBookableMessage = 'Für Teilnehmer:in {{ number }} ist keine Buchung möglich';
|
||||
|
||||
public function getTargets(): array|string
|
||||
{
|
||||
return static::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantEditDto;
|
||||
use App\Service\ParticipantEligibilityChecker;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
/**
|
||||
* Enforces ski pass selection for participants that need one.
|
||||
*
|
||||
* Three outcomes, mirroring what the form actually renders:
|
||||
*
|
||||
* - Ineligible participant: no ski pass is obtainable, so the ski pass field is not rendered
|
||||
* at all. Asking for a selection would be unsatisfiable - the booking is rejected as not
|
||||
* bookable instead.
|
||||
* - Ski pass required and none selected: the regular "please select" violation.
|
||||
* - Ski pass not required (baby, or a travel that offers no ski passes): no violation.
|
||||
*/
|
||||
class SkiPassSelectionValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ParticipantEligibilityChecker $participantEligibilityService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
if (false === $constraint instanceof SkiPassSelection) {
|
||||
throw new UnexpectedTypeException($constraint, SkiPassSelection::class);
|
||||
}
|
||||
|
||||
if (false === $value instanceof ParticipantEditDto) {
|
||||
throw new UnexpectedTypeException($value, ParticipantEditDto::class);
|
||||
}
|
||||
|
||||
// Skip validation in edit submissions - ski pass is readonly there.
|
||||
if (BookingDto::MODE_EDIT === $value->bookingContext->getMode()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$participant = $value->participant;
|
||||
|
||||
// Without a birth date nothing can be evaluated - the date of birth constraint reports it
|
||||
if (null === $participant->dateOfBirth) {
|
||||
return;
|
||||
}
|
||||
|
||||
$participantIndex = $participant->index ?? 0;
|
||||
|
||||
if (false === $this->participantEligibilityService->isParticipantEligible($value->bookingContext, $participantIndex)) {
|
||||
$this->context->buildViolation($constraint->notBookableMessage)
|
||||
->setParameter('{{ number }}', (string) ($participantIndex + 1))
|
||||
->atPath('participant.skiPass')
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (false === $this->participantEligibilityService->isSkiPassRequired($value->bookingContext, $participantIndex)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $participant->skiPass) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->atPath('participant.skiPass')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user