diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 96b52ec..7f3f974 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -5,12 +5,11 @@ namespace App\Controller; use App\Form\{CreateUserRequestType}; use App\FormRequest\CreateUserRequest; use App\Repository\InviteRepository; -use App\User\Exception\InvalidInviteException; use App\User\{InviteManager, UserManager}; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Form\Extension\Core\Type\SubmitType; -use Symfony\Component\Form\{FormError, FormInterface}; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\{Request, Response}; class UserController extends Controller @@ -26,30 +25,19 @@ class UserController extends Controller $createUserRequest = new CreateUserRequest($inviteCode); $form = $this->createRegisterForm($createUserRequest, $inviteCode); - $inviteInvalid = false; - - if (null === $invite = $inviteRepo->findOneBy(['code' => $inviteCode, 'usedBy' => null])) { - $inviteInvalid = true; - } + $invite = $inviteRepo->findOneBy(['code' => $inviteCode, 'usedBy' => null]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - try { - $user = $userManager->createUserByInvite( - $createUserRequest->username, - $createUserRequest->password, - $createUserRequest->email, - $invite - ); + $user = $userManager->createUserByInvite( + $createUserRequest->username, + $createUserRequest->password, + $createUserRequest->email, + $invite + ); - $inviteManager->createInvitesForUser($user); - } catch (InvalidInviteException $ex) { - // @FIXME refactor InvalidInviteException to proper validator - $form->get('inviteCode')->addError(new FormError('Invalid invite code')); - - return $this->render('User/register.html.twig', ['form' => $form->createView()]); - } + $inviteManager->createInvitesForUser($user); $em->flush(); @@ -58,7 +46,7 @@ class UserController extends Controller return $this->render('User/register.html.twig', [ 'form' => $form->createView(), - 'inviteInvalid' => $inviteInvalid, + 'inviteValid' => $invite ? true : null, ]); } 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/User/Exception/InvalidInviteException.php b/src/User/Exception/InvalidInviteException.php index be497ae..827015a 100644 --- a/src/User/Exception/InvalidInviteException.php +++ b/src/User/Exception/InvalidInviteException.php @@ -2,7 +2,7 @@ namespace App\User\Exception; -class InvalidInviteException extends \Exception +class InvalidInviteException extends \InvalidArgumentException { protected $message = 'Invalid invite'; } \ No newline at end of file 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 diff --git a/templates/User/register.html.twig b/templates/User/register.html.twig index 600b184..472ee49 100644 --- a/templates/User/register.html.twig +++ b/templates/User/register.html.twig @@ -1,11 +1,11 @@ {% extends 'base.html.twig' %} {% block content %} - {% if inviteInvalid %} -

Invalid invite

- {% else %} + {% if inviteValid %}
{{ form(form) }}
+ {% else %} +

Invalid invite

{% endif %} {% endblock %} \ No newline at end of file