| 
2 | 2 | 
 
  | 
3 | 3 | namespace App\Validator\Constraints;  | 
4 | 4 | 
 
  | 
5 |  | -use App\Entity\Conference;  | 
6 |  | -use App\Entity\User;  | 
 | 5 | +use App\Entity\Participation;  | 
7 | 6 | use App\Repository\ParticipationRepository;  | 
8 |  | -use Symfony\Component\Security\Core\Security;  | 
9 | 7 | use Symfony\Component\Validator\Constraint;  | 
10 | 8 | use Symfony\Component\Validator\ConstraintValidator;  | 
11 | 9 | use Symfony\Component\Validator\Exception\UnexpectedValueException;  | 
12 | 10 | 
 
  | 
13 | 11 | class NoParticipationDuplicateValidator extends ConstraintValidator  | 
14 | 12 | {  | 
15 | 13 |     public function __construct(  | 
16 |  | -        private Security $security,  | 
17 | 14 |         private ParticipationRepository $participationRepository,  | 
18 | 15 |     ) {  | 
19 | 16 |     }  | 
20 | 17 | 
 
  | 
21 | 18 |     /**  | 
22 |  | -     * @param Conference|null          $conference  | 
 | 19 | +     * @param Participation|null       $participation  | 
23 | 20 |      * @param NoParticipationDuplicate $constraint  | 
24 | 21 |      */  | 
25 |  | -    public function validate($conference, Constraint $constraint): void  | 
 | 22 | +    public function validate($participation, Constraint $constraint): void  | 
26 | 23 |     {  | 
27 |  | -        if (!$conference) {  | 
 | 24 | +        if (!$participation) {  | 
28 | 25 |             return;  | 
29 | 26 |         }  | 
30 | 27 | 
 
  | 
31 |  | -        if (!$conference instanceof Conference) {  | 
32 |  | -            throw new UnexpectedValueException($conference, Conference::class);  | 
 | 28 | +        if (!$participation instanceof Participation) {  | 
 | 29 | +            throw new UnexpectedValueException($participation, Participation::class);  | 
33 | 30 |         }  | 
34 | 31 | 
 
  | 
35 |  | -        /** @var User $user */  | 
36 |  | -        $user = $this->security->getUser();  | 
37 | 32 |         $existingParticipation = $this->participationRepository->findOneBy([  | 
38 |  | -            'participant' => $user,  | 
39 |  | -            'conference' => $conference,  | 
 | 33 | +            'participant' => $participation->getParticipant(),  | 
 | 34 | +            'conference' => $participation->getConference(),  | 
40 | 35 |         ]);  | 
41 | 36 | 
 
  | 
42 | 37 |         if ($existingParticipation) {  | 
43 | 38 |             $this->context  | 
44 | 39 |                 ->buildViolation($constraint->message)  | 
45 |  | -                ->setParameter('{{ user_name }}', $user->getName())  | 
 | 40 | +                ->setParameter('{{ user_name }}', $participation->getParticipant()->getName())  | 
46 | 41 |                 ->addViolation()  | 
47 | 42 |             ;  | 
48 | 43 |         }  | 
 | 
0 commit comments