|
| 1 | +<?= "<?php\n" ?> |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace <?= $controller_ns ?>; |
| 6 | + |
| 7 | +use <?= $user_class ?>; |
| 8 | +use <?= $form_ns ?>\ForgotPasswordType; |
| 9 | +use MsgPhp\User\Command\RequestUserPassword; |
| 10 | +use Symfony\Component\Form\FormFactoryInterface; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 13 | +use Symfony\Component\HttpFoundation\Response; |
| 14 | +use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
| 15 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 16 | +use Symfony\Component\Routing\Annotation\Route; |
| 17 | +use Twig\Environment; |
| 18 | + |
| 19 | +/** |
| 20 | + * @Route("/forgot-password", name="forgot_password") |
| 21 | + */ |
| 22 | +final class ForgotPasswordController |
| 23 | +{ |
| 24 | + public function __invoke( |
| 25 | + Request $request, |
| 26 | + FormFactoryInterface $formFactory, |
| 27 | + FlashBagInterface $flashBag, |
| 28 | + Environment $twig, |
| 29 | + MessageBusInterface $bus |
| 30 | + ): Response { |
| 31 | + $form = $formFactory->createNamed('', ForgotPasswordType::class); |
| 32 | + $form->handleRequest($request); |
| 33 | + |
| 34 | + if ($form->isSubmitted() && $form->isValid()) { |
| 35 | + /** @var <?= $user_short_class ?> $user */ |
| 36 | + $user = $form->getData()['user']; |
| 37 | + $bus->dispatch(new RequestUserPassword($user->getId())); |
| 38 | + $flashBag->add('success', 'You\'re password is requested.'); |
| 39 | + |
| 40 | + return new RedirectResponse('/login'); |
| 41 | + } |
| 42 | + |
| 43 | + return new Response($twig->render('<?= $template_dir ?>forgot_password.html.twig', [ |
| 44 | + 'form' => $form->createView(), |
| 45 | + ])); |
| 46 | + } |
| 47 | +} |
0 commit comments