<?php
declare(strict_types=1);
namespace App\Controller\Auth;
use App\Model\Admin\Entity\Settings\Key;
use App\Model\User\UseCase\User\SignUp;
use App\ReadModel\Admin\Settings\SettingsFetcher;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class SignUpController extends AbstractController
{
private $logger;
private $translator;
public function __construct(
LoggerInterface $logger,
TranslatorInterface $translator
) {
$this->logger = $logger;
$this->translator = $translator;
}
/**
* @param Request $request
* @param SignUp\Request\Handler $handler
* @return Response
* @Route("/sign-up",name="auth.sign.up")
*/
public function request(Request $request, SignUp\Request\Handler $handler, SettingsFetcher $settingsFetcher): Response
{
$privacyPolicyLink = $settingsFetcher->findDetailByKey(Key::privacyPolicyLink());
$keySmartCaptchaClientKey = $settingsFetcher->findDetailByKey(Key::keySmartCaptchaClientKey());
$keySmartCaptchaStatus = $settingsFetcher->findDetailByKey(Key::keySmartCaptchaStatus());
$keySmartCaptchaTest = $settingsFetcher->findDetailByKey(Key::keySmartCaptchaTest());
$command = new SignUp\Request\Command($request->getClientIp() ?? '127.0.0.1');
$form = $this->createForm(SignUp\Request\Form::class, $command);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$smartToken = $request->request->get('smart-token') ?? null;
$command->smartToken = $smartToken;
$handler->handle($command);
$this->addFlash('success', $this->translator->trans("A letter has been sent to email %email%. follow the link to confirm your email.", ['%email%' => $command->email], 'exceptions'));
return $this->redirectToRoute('app_login');
} catch (\DomainException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
$this->addFlash('error', $this->translator->trans($e->getMessage(), [], 'exceptions'));
}
}
return $this->render('app/auth/signup.html.twig', [
'form' => $form->createView(),
'privacyPolicyLink' => $privacyPolicyLink,
'keySmartCaptchaClientKey' => $keySmartCaptchaClientKey,
'keySmartCaptchaStatus' => $keySmartCaptchaStatus,
'keySmartCaptchaTest' => $keySmartCaptchaTest,
]);
}
/**
* @param string $token
* @param SignUp\Confirm\ByToken\Handler $handler
* @return Response
* @Route("/sign-confirm/{token}", name="auth.sign.confirm")
*/
public function confirm(string $token, SignUp\Confirm\ByToken\Handler $handler): Response
{
try {
$handler->handle(new SignUp\Confirm\ByToken\Command($token));
$this->addFlash('success', $this->translator->trans('Email is successfully confirmed.', [], 'exceptions'));
return $this->redirectToRoute('app_login');
} catch (\DomainException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
$this->addFlash('error', $this->translator->trans($e->getMessage(), [], 'exceptions'));
return $this->redirectToRoute('app_login');
}
}
}