| Total Complexity | 10 |
| Total Lines | 30 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | class UniqueUsernameValidator extends ConstraintValidator { |
||
| 13 | |||
| 14 | public function __construct(private UserRepositoryInterface $userRepository) |
||
| 15 | { |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @inheritDoc |
||
| 20 | */ |
||
| 21 | public function validate($value, Constraint $constraint) { |
||
| 22 | if(!$constraint instanceof UniqueUsername) { |
||
| 23 | throw new UnexpectedTypeException($constraint, UniqueUsername::class); |
||
| 24 | } |
||
| 25 | |||
| 26 | if(!is_string($value)) { |
||
| 27 | throw new UnexpectedTypeException($constraint, 'string'); |
||
| 28 | } |
||
| 29 | |||
| 30 | $user = $this->userRepository->findOneByUsername($value); |
||
| 31 | |||
| 32 | if($user !== null && $this->matchesType($user, $constraint->type) === false) { |
||
| 33 | $this->context |
||
| 34 | ->buildViolation($constraint->message) |
||
| 35 | ->addViolation(); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | private function matchesType(User $user, string $type) { |
||
| 42 | } |
||
| 43 | } |