<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use App\Repository\CategoryRepository;
use App\Repository\ProductRepository;
use App\Repository\UserRepository;
use App\Security\EmailVerifier;
use App\Services\Cart;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class RegistrationController extends AbstractController
{
private EmailVerifier $emailVerifier;
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}
#[Route('/account', name: 'account')]
public function account(Request $request, ProductRepository $productRepo, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, AuthenticationUtils $authenticationUtils, CategoryRepository $catRepo): Response
{
$categories = $catRepo->findAll();
//TO COPY TO EVERY ROUTE THAT DISPLAY CART
$session = $request->getSession();
$cart = new Cart($session);
$totalQuantity = $cart->getCartTotalQuantity();
$maxQuantity = $cart->getMaxQuantityAllowed();
$cartProducts = $cart->getCartProducts($productRepo);
$totalPrice = $cart->getTotalPrice($productRepo);
//REGISTER SYSTEM SECTION
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('no-reply@lestresorsdeluna.fr', 'Luna Mail Bot'))
->to($user->getEmail())
->subject('Veuillez confirmer votre email')
->htmlTemplate('emails/confirmation_email.html.twig')
);
// do anything else you need here, like send an email
$this->addFlash('message', 'Création de compte réussie, un mail de confirmation vous a été envoyé');
return $this->redirectToRoute('app_home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('account/account.html.twig', [
'categories' => $categories,
'registrationForm' => $form->createView(),
'last_username' => $lastUsername,
'error' => $error,
//TO COPY TO EVERY ROUTE THAT DISPLAY CART
'totalQuantity' => $totalQuantity,
'maxQuantity' => $maxQuantity,
'cartProducts' => $cartProducts,
'totalPrice' => $totalPrice
]);
}
#[Route('/register-test', name: 'app_register_base')]
public function registerTest(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('no-reply@lestresorsdeluna.fr', 'Luna Mail Bot'))
->to($user->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig')
);
// do anything else you need here, like send an email
return $this->redirectToRoute('app_home');
}
return $this->render('registration/register_base.html.twig', [
'registrationForm' => $form->createView(),
]);
}
#[Route('/verify/email', name: 'app_verify_email')]
public function verifyUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository): Response
{
$id = $request->query->get('id');
if (null === $id) {
return $this->redirectToRoute('account');
}
$user = $userRepository->find($id);
if (null === $user) {
return $this->redirectToRoute('account');
}
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
return $this->redirectToRoute('account');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->addFlash('success', 'Votre email à été vérifié.');
return $this->redirectToRoute('app_home');
}
#[Route('generate-confirmation-link', name: 'generate_confirmation_list')]
public function generateConfirmationLink()
{
$user = $this->getUser();
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
->from(new Address('no-reply@lestresorsdeluna.fr', 'Luna Mail Bot'))
->to($user->getEmail())
->subject('Veuillez confirmer votre email')
->htmlTemplate('emails/confirmation_email.html.twig')
);
return $this->redirectToRoute('app_home');
}
}