55namespace PhpList \RestBundle \Validator ;
66
77use PhpList \Core \Domain \Repository \Subscription \SubscriberRepository ;
8+ use PhpList \RestBundle \Validator \Constraint \UniqueEmail ;
89use Symfony \Component \Validator \Constraint ;
910use Symfony \Component \Validator \ConstraintValidator ;
11+ use Symfony \Component \Validator \Exception \UnexpectedTypeException ;
12+ use Symfony \Component \Validator \Exception \UnexpectedValueException ;
1013
1114class UniqueEmailValidator extends ConstraintValidator
1215{
@@ -17,19 +20,26 @@ public function __construct(SubscriberRepository $subscriberRepository)
1720 $ this ->subscriberRepository = $ subscriberRepository ;
1821 }
1922
20- public function validate ($ value , Constraint $ constraint )
23+ public function validate ($ value , Constraint $ constraint ): void
2124 {
22- /* @var $constraint UniqueEmail */
25+ if (!$ constraint instanceof UniqueEmail) {
26+ throw new UnexpectedTypeException ($ constraint , UniqueEmail::class);
27+ }
2328
2429 if (null === $ value || '' === $ value ) {
2530 return ;
2631 }
2732
28- if ($ this ->subscriberRepository ->findOneByEmail ($ value )) {
33+ if (!is_string ($ value )) {
34+ throw new UnexpectedValueException ($ value , 'string ' );
35+ }
36+
37+ $ existingUser = $ this ->subscriberRepository ->findOneBy (['email ' => $ value ]);
38+
39+ if ($ existingUser ) {
2940 $ this ->context ->buildViolation ($ constraint ->message )
3041 ->setParameter ('{{ value }} ' , $ value )
3142 ->addViolation ();
3243 }
3344 }
3445}
35-
0 commit comments