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