<?php
declare(strict_types=1);
namespace App\UI\Merchant\Controller;
use App\Application\Merchant\Command\SendMerchantForgotPasswordEmail\SendMerchantForgotPasswordEmailCommand;
use App\Infrastructure\Shared\Bus\Command\CommandBusInterface;
use App\UI\Merchant\Form\Type\ForgotPasswordType;
use App\UI\Shared\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class ForgotPasswordController extends AbstractController
{
#[Route(path: '/forgot-password', name: 'merchant_forgot_password')]
public function index(
Request $request,
CommandBusInterface $commandBus,
TranslatorInterface $translator
): Response {
$form = $this->createForm(ForgotPasswordType::class, null, [
'action' => $this->generateUrl('merchant_forgot_password'),
]);
$template = 'merchant/forgot_password/index.html.twig';
$form->handleRequest($request);
if ($form->isSubmitted()) {
if (!$form->isValid()) {
return $this->createFormErrorsJsonResponse($form);
}
$email = $form->get('email')->getData();
$commandBus->dispatch(new SendMerchantForgotPasswordEmailCommand($email));
return $this->render($template, [
'message' => $translator->trans('check_email', ['%email%' => $email]),
]);
}
return $this->renderForm($template, [
'form' => $form,
]);
}
}