src/Core/Action/Http/ForgottenPasswordAction.php line 33

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2020 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2020 TECLA Consulting Group oü
  16.  */
  17. namespace App\Core\Action\Http;
  18. use App\Core\Event\UserPasswordResetRequestEvent;
  19. use App\Core\Form\Type\ForgottenPasswordRequestFormType;
  20. use App\Core\Query\User\FindOneByIdentifierQueryInterface;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\Form\FormFactoryInterface;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\RouterInterface;
  28. use Twig\Environment;
  29. final class ForgottenPasswordAction
  30. {
  31.     private FormFactoryInterface $formFactory;
  32.     private FindOneByIdentifierQueryInterface $findUserQuery;
  33.     private EventDispatcherInterface $eventDispatcher;
  34.     private RequestStack $requestStack;
  35.     private RouterInterface $router;
  36.     private Environment $twig;
  37.     public function __construct(
  38.         FormFactoryInterface $formFactory,
  39.         FindOneByIdentifierQueryInterface $findUserQuery,
  40.         EventDispatcherInterface $eventDispatcher,
  41.         RequestStack $requestStack,
  42.         RouterInterface $router,
  43.         Environment $twig
  44.     ) {
  45.         $this->formFactory $formFactory;
  46.         $this->findUserQuery $findUserQuery;
  47.         $this->eventDispatcher $eventDispatcher;
  48.         $this->requestStack $requestStack;
  49.         $this->router $router;
  50.         $this->twig $twig;
  51.     }
  52.     public function __invoke(Request $request): Response
  53.     {
  54.         $form $this->formFactory->create(ForgottenPasswordRequestFormType::class);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             $data $form->getData();
  58.             if (null !== $user $this->findUserQuery->execute($data['username'])) {
  59.                 $event = new UserPasswordResetRequestEvent($user);
  60.                 $this->eventDispatcher->dispatch($event);
  61.             }
  62.             $this->requestStack->getSession()->set('app_forgotten_password.email'$data['username']);
  63.             return new RedirectResponse($this->router->generate('app.core.http.forgotten_password.email_sent'));
  64.         }
  65.         return new Response($this->twig->render('packages/core/action/http/forgotten_password.html.twig', [
  66.             'form' => $form->createView(),
  67.         ]));
  68.     }
  69. }