- <?php
- namespace App\Controller;
- use App\Entity\User;
- use App\Security\EmailVerifier;
- use App\Form\RegistrationFormType;
- use App\Repository\UserRepository;
- use Symfony\Component\Mime\Address;
- use Symfony\Bridge\Twig\Mime\TemplatedEmail;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Contracts\Translation\TranslatorInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
- use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Doctrine\ORM\EntityManagerInterface;
- class RegistrationController extends AbstractController
- {
-     private $emailVerifier;
-     private $params;
-     private $manager;
-     public function __construct(EmailVerifier $emailVerifier, ParameterBagInterface $params, EntityManagerInterface $manager)
-     {
-         $this->emailVerifier = $emailVerifier;
-         $this->params = $params;
-         $this->manager = $manager;
-     }
-     /**
-      * @Route("/register", name="app_register")
-      */
-     public function register(Request $request, UserPasswordHasherInterface $userPasswordHasherInterface, TranslatorInterface $translator): Response
-     {
-         $user = new User();
-         $form = $this->createForm(RegistrationFormType::class, $user);
-         $form->handleRequest($request);
-         if ($form->isSubmitted() && $form->isValid()) {
-             // encode the plain password
-             $user->setPassword(
-             $userPasswordHasherInterface->hashPassword(
-                     $user,
-                     $form->get('plainPassword')->getData()
-                 )
-             );
-             //set active lang and persote user
-             $user->setLang($request->getLocale());
-             $this->manager->persist($user);
-             $this->manager->flush();
-             // generate a signed url and email it to the user
-             $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
-                 (new TemplatedEmail())
-                     ->from(new Address($this->params->get('app.sendermail'), $this->params->get('app.title')))
-                     ->to($user->getEmail())
-                     ->subject($translator->trans('register.subject', [], 'email'))
-                     ->htmlTemplate('registration/confirmation_email.html.twig')
-             );
-             return $this->redirectToRoute('app_register_verify');
-         }
-         return $this->render('registration/register.html.twig', [
-             'registrationForm' => $form->createView(),
-         ]);
-     }
-     /**
-      * @Route("/verify/email", name="app_verify_email")
-      */
-     public function verifyUserEmail(Request $request, UserRepository $userRepository, TranslatorInterface $translator): Response
-     {
-         $id = $request->get('id'); // retrieve the user id from the url
-         // Verify the user id exists and is not null
-         if (null === $id) {
-             return $this->redirectToRoute('app_register');
-         }
-         $user = $userRepository->find($id);
-         // Ensure the user exists in persistence
-         if (null === $user) {
-             return $this->redirectToRoute('app_register');
-         }
-         // validate email confirmation link, sets User::isVerified=true and persists
-         try {
-             $this->emailVerifier->handleEmailConfirmation($request, $user);
-         } catch (VerifyEmailExceptionInterface $exception) {
-             $this->addFlash('verify_email_error', $exception->getReason());
-             return $this->redirectToRoute('app_register');
-         }
-         // flash and redirect
-         $this->addFlash('success', $translator->trans('register.confirm'));
-         return $this->redirectToRoute('app_login');
-     }
-     /**
-      * @Route("/register/verify", name="app_register_verify")
-      */
-     public function verifyMsg(): Response
-     {
-         return $this->render('registration/confirm.html.twig');
-     }
- }
-