src/Controller/RegistrationController.php line 32
<?php
namespace App\Controller;
use App\Entity\UserEntity;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Services\FlagsService;
use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;
class RegistrationController extends AbstractController
{
private $translator;
private $flagsService;
private $mailer;
public function __construct(TranslatorInterface $translator, FlagsService $flagsService)
{
$this->translator = $translator;
$this->flagsService = $flagsService;
// Configurtion of the mailer Swift
$transport = (new Swift_SmtpTransport($_ENV['MAILSERVER'], $_ENV['MAILPORT'], 'ssl'))
->setUsername($_ENV['EMAIL'])
->setPassword($_ENV['PASSWORD']);
$transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
// Create the Mailer using your created Transport
$this->mailer = new Swift_Mailer($transport);
}
/**
* @Route("/{_locale}/register", name="register")
*/
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new UserEntity();
$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()
)
);
$user->setActive(false);
$user->setRoles(['ROLE_USER']);
$user->setMd5User(md5($user->getEmail()));
// Send email to user
if($this->sendEmail($user->getEmail(), $user->getMd5User(), $request->getLocale())) {
// Save user in database
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('message', $this->translator->trans('user_created_check_email'));
$this->addFlash('color', 'success');
return $this->redirectToRoute('login', ['_locale' => $request->getLocale()]);
}
$this->addFlash('message', $this->translator->trans('error_sending_email'));
$this->addFlash('color', 'secondary');
return $this->redirectToRoute('register', ['_locale' => $request->getLocale()]);
}
return $this->render('register.html.twig', [
'form' => $form->createView(),
'flags' => $this->flagsService->getflags(),
]);
}
public function sendEmail(string $userEmail, string $md5Email, string $locale): bool
{
// Message
$message = (new Swift_Message($this->translator->trans('welcome_mail')))
->setFrom('register@simplexebooks.com')
->setTo($userEmail)
->setBody(
$this->translator->trans('mail_create_account_message_one')."<a href='https://simplexebooks.com/$locale/activate-account/$md5Email'>".
$this->translator->trans('mail_create_account_message_two')."
<p>https://simplexebooks.com/$locale/activate-account/$md5Email</p>".
$this->translator->trans('mail_create_account_message_three'),
'text/html'
);
try {
$this->mailer->send($message);
return true;
} catch (\Exception) {
return false;
}
}
}