5
5
namespace PhpList \RestBundle \Validator ;
6
6
7
7
use PhpList \Core \Domain \Repository \Subscription \SubscriberRepository ;
8
+ use PhpList \RestBundle \Validator \Constraint \UniqueEmail ;
8
9
use Symfony \Component \Validator \Constraint ;
9
10
use Symfony \Component \Validator \ConstraintValidator ;
11
+ use Symfony \Component \Validator \Exception \UnexpectedTypeException ;
12
+ use Symfony \Component \Validator \Exception \UnexpectedValueException ;
10
13
11
14
class UniqueEmailValidator extends ConstraintValidator
12
15
{
@@ -17,19 +20,26 @@ public function __construct(SubscriberRepository $subscriberRepository)
17
20
$ this ->subscriberRepository = $ subscriberRepository ;
18
21
}
19
22
20
- public function validate ($ value , Constraint $ constraint )
23
+ public function validate ($ value , Constraint $ constraint ): void
21
24
{
22
- /* @var $constraint UniqueEmail */
25
+ if (!$ constraint instanceof UniqueEmail) {
26
+ throw new UnexpectedTypeException ($ constraint , UniqueEmail::class);
27
+ }
23
28
24
29
if (null === $ value || '' === $ value ) {
25
30
return ;
26
31
}
27
32
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 ) {
29
40
$ this ->context ->buildViolation ($ constraint ->message )
30
41
->setParameter ('{{ value }} ' , $ value )
31
42
->addViolation ();
32
43
}
33
44
}
34
45
}
35
-
0 commit comments