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 Apr 19, 2022
1 parent 157579f commit c1c2178
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Entity/Participation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* @ORM\Entity()
* @CustomAssert\NoParticipationDuplicate()
*/
class Participation
{
Expand Down Expand Up @@ -45,7 +46,6 @@ class Participation
* @ORM\ManyToOne(targetEntity="App\Entity\Conference", inversedBy="participations", cascade={"persist"})
* @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 @@ -17,4 +17,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 @@ -12,6 +12,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 @@ -22,36 +23,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 c1c2178

Please sign in to comment.