diff --git a/src/Entity/Participation.php b/src/Entity/Participation.php index 925145ca..cd650693 100644 --- a/src/Entity/Participation.php +++ b/src/Entity/Participation.php @@ -9,6 +9,8 @@ /** * @ORM\Entity() + * + * @CustomAssert\NoParticipationDuplicate() */ class Participation { @@ -39,8 +41,6 @@ class Participation * * @ORM\JoinColumn(nullable=false) * - * @CustomAssert\NoParticipationDuplicate() - * * @CustomAssert\NotEndedConference() */ private Conference $conference; diff --git a/src/Validator/Constraints/NoParticipationDuplicate.php b/src/Validator/Constraints/NoParticipationDuplicate.php index f8138015..e006d327 100644 --- a/src/Validator/Constraints/NoParticipationDuplicate.php +++ b/src/Validator/Constraints/NoParticipationDuplicate.php @@ -8,4 +8,9 @@ class NoParticipationDuplicate extends Constraint { public string $message = 'A participation at this conference is already registered for {{ user_name }}.'; + + public function getTargets() + { + return self::CLASS_CONSTRAINT; + } } diff --git a/src/Validator/Constraints/NoParticipationDuplicateValidator.php b/src/Validator/Constraints/NoParticipationDuplicateValidator.php index a175ea3e..14367ca7 100644 --- a/src/Validator/Constraints/NoParticipationDuplicateValidator.php +++ b/src/Validator/Constraints/NoParticipationDuplicateValidator.php @@ -2,10 +2,8 @@ namespace App\Validator\Constraints; -use App\Entity\Conference; -use App\Entity\User; +use App\Entity\Participation; use App\Repository\ParticipationRepository; -use Symfony\Component\Security\Core\Security; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedValueException; @@ -13,36 +11,33 @@ class NoParticipationDuplicateValidator extends ConstraintValidator { public function __construct( - private Security $security, private ParticipationRepository $participationRepository, ) { } /** - * @param Conference|null $conference + * @param Participation|null $participation * @param NoParticipationDuplicate $constraint */ - public function validate($conference, Constraint $constraint): void + public function validate($participation, Constraint $constraint): void { - if (!$conference) { + if (!$participation) { return; } - if (!$conference instanceof Conference) { - throw new UnexpectedValueException($conference, Conference::class); + if (!$participation instanceof Participation) { + throw new UnexpectedValueException($participation, Participation::class); } - /** @var User $user */ - $user = $this->security->getUser(); $existingParticipation = $this->participationRepository->findOneBy([ - 'participant' => $user, - 'conference' => $conference, + 'participant' => $participation->getParticipant(), + 'conference' => $participation->getConference(), ]); if ($existingParticipation) { $this->context ->buildViolation($constraint->message) - ->setParameter('{{ user_name }}', $user->getName()) + ->setParameter('{{ user_name }}', $participation->getParticipant()->getName()) ->addViolation() ; }