Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add participation validation #270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Entity/Participation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* @ORM\Entity()
*
* @CustomAssert\NoParticipationDuplicate()
*/
class Participation
{
Expand Down Expand Up @@ -39,8 +41,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;
}
}
23 changes: 9 additions & 14 deletions src/Validator/Constraints/NoParticipationDuplicateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,42 @@

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;

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