src/Controller/RegistrationController.php line 32

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\UserEntity;
  4. use App\Form\RegistrationFormType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use App\Services\FlagsService;
  13. use Swift_SmtpTransport;
  14. use Swift_Mailer;
  15. use Swift_Message;
  16. class RegistrationController extends AbstractController
  17. {   
  18.     
  19.     private $translator;
  20.     private $flagsService;
  21.     private $mailer;
  22.     
  23.     public function __construct(TranslatorInterface $translatorFlagsService $flagsService)
  24.     {
  25.         $this->translator $translator;
  26.         $this->flagsService $flagsService;
  27.         // Configurtion of the mailer Swift
  28.         $transport = (new Swift_SmtpTransport($_ENV['MAILSERVER'], $_ENV['MAILPORT'], 'ssl'))
  29.             ->setUsername($_ENV['EMAIL'])
  30.             ->setPassword($_ENV['PASSWORD']);
  31.         $transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true'verify_peer' => false)));
  32.         // Create the Mailer using your created Transport
  33.         $this->mailer = new Swift_Mailer($transport);
  34.     }
  35.     
  36.     /**
  37.     * @Route("/{_locale}/register", name="register")
  38.     */
  39.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  40.     {
  41.         $user = new UserEntity();
  42.         $form $this->createForm(RegistrationFormType::class, $user);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             // encode the plain password
  46.             $user->setPassword(
  47.                 $userPasswordHasher->hashPassword(
  48.                     $user,
  49.                     $form->get('plainPassword')->getData()
  50.                 )
  51.             );
  52.             $user->setActive(false);
  53.             $user->setRoles(['ROLE_USER']);
  54.             $user->setMd5User(md5($user->getEmail()));
  55.             
  56.             // Send email to user
  57.             if($this->sendEmail($user->getEmail(), $user->getMd5User(), $request->getLocale())) {
  58.                 
  59.                 // Save user in database
  60.                 $entityManager->persist($user);
  61.                 $entityManager->flush();            
  62.                 $this->addFlash('message'$this->translator->trans('user_created_check_email'));
  63.                 $this->addFlash('color''success');
  64.                 return $this->redirectToRoute('login', ['_locale' => $request->getLocale()]);
  65.             } 
  66.             
  67.             $this->addFlash('message'$this->translator->trans('error_sending_email'));
  68.             $this->addFlash('color''secondary');
  69.             return $this->redirectToRoute('register', ['_locale' => $request->getLocale()]);
  70.         }
  71.         return $this->render('register.html.twig', [
  72.             'form' => $form->createView(),
  73.             'flags' => $this->flagsService->getflags(),
  74.         ]);
  75.     }
  76.     public function sendEmail(string $userEmailstring $md5Emailstring $locale): bool
  77.     {   
  78.         // Message
  79.         $message = (new Swift_Message($this->translator->trans('welcome_mail')))
  80.             ->setFrom('register@simplexebooks.com')
  81.             ->setTo($userEmail)
  82.             ->setBody(
  83.                 $this->translator->trans('mail_create_account_message_one')."<a href='https://simplexebooks.com/$locale/activate-account/$md5Email'>".
  84.                 $this->translator->trans('mail_create_account_message_two')."
  85.                 <p>https://simplexebooks.com/$locale/activate-account/$md5Email</p>".
  86.                 $this->translator->trans('mail_create_account_message_three'),
  87.                 'text/html'
  88.             );
  89.         try {
  90.             $this->mailer->send($message);
  91.             return true;
  92.         } catch (\Exception) {
  93.             return false;
  94.         }
  95.     }
  96. }