src/Core/EventSubscriber/RegistrationEmailConfirmationSubscriber.php line 73

  1. <?php
  2. /**
  3.  * Copyright (c) 2019 TECLA Consulting Group oü.
  4.  * All rights reserved.
  5.  *
  6.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  7.  * All rights reserved. The methods and
  8.  * techniques described herein are considered trade secrets
  9.  * and/or confidential. Reproduction or distribution, in whole
  10.  * or in part, is forbidden except by express written permission
  11.  * of TECLA Consulting Group oü.
  12.  *
  13.  * @author    Matúš Sýkorjak <matus@tecla.no>
  14.  * @copyright 2019 TECLA Consulting Group oü
  15.  */
  16. namespace App\Core\EventSubscriber;
  17. use App\Core\Event\UserRegistrationSuccessEvent;
  18. use JetBrains\PhpStorm\ArrayShape;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\Mailer\MailerInterface;
  23. use Symfony\Component\Mime\Address;
  24. use Symfony\Component\Mime\Email;
  25. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. use Twig\Environment;
  28. class RegistrationEmailConfirmationSubscriber implements EventSubscriberInterface
  29. {
  30.     private UrlGeneratorInterface $router;
  31.     private RequestStack $requestStack;
  32.     private MailerInterface $mailer;
  33.     private Environment $twig;
  34.     private TranslatorInterface $translator;
  35.     private int $unverifiedUsersLifetimeDays;
  36.     private string $appName;
  37.     public function __construct(
  38.         UrlGeneratorInterface $router,
  39.         RequestStack $requestStack,
  40.         MailerInterface $mailer,
  41.         Environment $twig,
  42.         TranslatorInterface $translator,
  43.         int $unverifiedUsersLifetimeDays,
  44.         string $appName
  45.     ) {
  46.         $this->router $router;
  47.         $this->requestStack $requestStack;
  48.         $this->mailer $mailer;
  49.         $this->twig $twig;
  50.         $this->translator $translator;
  51.         $this->unverifiedUsersLifetimeDays $unverifiedUsersLifetimeDays;
  52.         $this->appName $appName;
  53.     }
  54.     #[ArrayShape([UserRegistrationSuccessEvent::class => 'string'])]
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             UserRegistrationSuccessEvent::class => 'onRegistrationSuccess',
  59.         ];
  60.     }
  61.     public function onRegistrationSuccess(UserRegistrationSuccessEvent $event): void
  62.     {
  63.         $user $event->getUser();
  64.         if (null === $user->getConfirmationToken()) {
  65.             return;
  66.         }
  67.         $confirmationUrl $this->router->generate('app_register_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
  68.         $templateHtml $this->twig->load('packages/core/email/email_confirmation.html.twig');
  69.         $templateTxt $this->twig->load('packages/core/email/email_confirmation.txt.twig');
  70.         $message = (new Email())
  71.             ->from(new Address('no-reply@tecla.cloud'$this->appName))
  72.             ->to($user->getEmail())
  73.             ->subject($this->translator->trans('title.confirm_email', [], 'core@messages'))
  74.             ->html($templateHtml->render([
  75.                 'confirmation_url' => $confirmationUrl,
  76.                 'contact' => $user->getContact(),
  77.                 'expiryLimit' => $this->unverifiedUsersLifetimeDays,
  78.             ]))
  79.             ->text($templateTxt->render([
  80.                 'confirmation_url' => $confirmationUrl,
  81.                 'contact' => $user->getContact(),
  82.                 'expiryLimit' => $this->unverifiedUsersLifetimeDays,
  83.             ]))
  84.         ;
  85.         $this->mailer->send($message);
  86.         $this->requestStack->getSession()->set('app_send_confirmation_email/email'$user->getEmail());
  87.         $url $this->router->generate('app.core.http.registration.email_sent');
  88.         $event->setResponse(new RedirectResponse($url));
  89.     }
  90. }