From 01827680a17b15b10c22a6cf1e37165dd7a0ff13 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sat, 30 Jun 2018 02:52:53 +0300 Subject: [PATCH] #4 ValidInvite constraint implemented. --- src/FormRequest/CreateUserRequest.php | 2 + src/Validator/Constraints/ValidInvite.php | 19 +++++++++ .../Constraints/ValidInviteValidator.php | 42 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/Validator/Constraints/ValidInvite.php create mode 100644 src/Validator/Constraints/ValidInviteValidator.php diff --git a/src/FormRequest/CreateUserRequest.php b/src/FormRequest/CreateUserRequest.php index 5e0b740..523429c 100644 --- a/src/FormRequest/CreateUserRequest.php +++ b/src/FormRequest/CreateUserRequest.php @@ -3,6 +3,7 @@ namespace App\FormRequest; use Symfony\Component\Validator\Constraints as Assert; +use App\Validator\Constraints as AppAssert; /** * @todo implement UniqueEntity constraint for DTO and use it here @@ -37,6 +38,7 @@ class CreateUserRequest * * @Assert\NotBlank() * @Assert\Length(min="32", max="32") + * @AppAssert\ValidInvite() */ public $inviteCode; diff --git a/src/Validator/Constraints/ValidInvite.php b/src/Validator/Constraints/ValidInvite.php new file mode 100644 index 0000000..1a0d455 --- /dev/null +++ b/src/Validator/Constraints/ValidInvite.php @@ -0,0 +1,19 @@ +inviteRepo = $inviteRepo; + } + + /** + * @param mixed $value + * @param ValidInvite $constraint + */ + public function validate($value, Constraint $constraint) + { + /** @var Invite $invite */ + if (null === $invite = $this->inviteRepo->findOneBy(['code' => $value])) { + $this->context->buildViolation($constraint->notFoundMessage) + ->setParameter('{{ code }}', $value) + ->addViolation() + ; + + return; + } + + if (null !== $invite->getUsedBy()) { + $this->context->buildViolation($constraint->usedMessage) + ->setParameter('{{ code }}', $value) + ->addViolation() + ; + } + } +} \ No newline at end of file