Skip to content

Commit

Permalink
Fix add participation validation
Browse files Browse the repository at this point in the history
  • Loading branch information
welcoMattic committed Nov 15, 2023
1 parent a711c0e commit 8bac329
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/Entity/Participation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @ORM\Entity()
* @CustomAssert\NoParticipationDuplicate()
*/
class Participation
{
Expand Down Expand Up @@ -39,8 +40,6 @@ class Participation
*
* @ORM\JoinColumn(nullable=false)
*
* @CustomAssert\NoParticipationDuplicate()
*
* @CustomAssert\NotEndedConference()
*/
private Conference $conference;
Expand Down
5 changes: 5 additions & 0 deletions src/Validator/Constraints/NoParticipationDuplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
20 changes: 9 additions & 11 deletions src/Validator/Constraints/NoParticipationDuplicateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Validator\Constraints;

use App\Entity\Conference;
use App\Entity\Participation;
use App\Entity\User;
use App\Repository\ParticipationRepository;
use Symfony\Component\Security\Core\Security;
Expand All @@ -13,36 +14,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()
;
}
Expand Down

0 comments on commit 8bac329

Please sign in to comment.