src/Subscriber/User/Registration/EmailSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\User\Registration;
  3. use App\Dictionary\MailPriority;
  4. use App\Event\Account\EmailChangedEvent;
  5. use App\Event\Account\PasswordChangedEvent;
  6. use App\Event\Account\PrepareEmailChangeEvent;
  7. use App\Event\Account\PreparePasswordChangeEvent;
  8. use App\Event\User\RegistrationEvent;
  9. use App\Service\Mail\MailService;
  10. use App\Service\Media\MemberMediaService;
  11. use App\Service\User\AccountService;
  12. use App\Service\User\Layer\AmateurLayer;
  13. use App\Subscriber\AbstractMailSubscriber;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Twig\Environment;
  18. class EmailSubscriber extends AbstractMailSubscriber implements EventSubscriberInterface
  19. {
  20.     public const ID_TOKEN_SEPARATOR ';';
  21.     protected RouterInterface $router;
  22.     protected MemberMediaService $mediaService;
  23.     protected AmateurLayer $amateurLayer;
  24.     public function __construct(RouterInterface $routerMemberMediaService $mediaServiceAmateurLayer $amateurLayer,
  25.         AccountService $accountServiceMailService $mailServiceEnvironment $twig)
  26.     {
  27.         parent::__construct($accountService$mailService$twig);
  28.         $this->router $router;
  29.         $this->mediaService $mediaService;
  30.         $this->amateurLayer $amateurLayer;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             RegistrationEvent::class => [
  36.                 ['onRegistrationSendEmail'0],
  37.             ],
  38.             PreparePasswordChangeEvent::class => [
  39.                 ['onPreparePasswordChangeSendConfirmEmail'0],
  40.             ],
  41.             PrepareEmailChangeEvent::class => [
  42.                 ['onPrepareEmailChangeSendConfirmEmail'0],
  43.             ],
  44.             EmailChangedEvent::class => [
  45.                 ['onEmailChangedSendInfoEmail'0],
  46.             ],
  47.             PasswordChangedEvent::class => [
  48.                 ['onPasswordChangedSendInfoEmail'0],
  49.             ],
  50.         ];
  51.     }
  52.     public function onRegistrationSendEmail(RegistrationEvent $event): bool
  53.     {
  54.         $account $event->getAccount();
  55.         if (!$event->getSendEmail()) {
  56.             return false;
  57.         }
  58.         $confirmationUrl $this->router->generate('frivol_account_register_confirm', [
  59.             'confirmationToken' => $account->getConfirmationToken(),
  60.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  61.         $params = [
  62.             'account' => $account,
  63.             'password' => $event->getPassword(),
  64.             'amateurs' => array_slice($this->getTopAmateurs(), 04),
  65.             'login' => $this->getAccountService()->getAutologinParameter($account),
  66.             'confirmationUrl' => $confirmationUrl,
  67.             'pin' => $account->getEmailToken(),
  68.         ];
  69.         $mail = new \Swift_Message('Willkommen bei Frivol.com');
  70.         $mail->addTo($account->getEmail());
  71.         $mail->setFrom('support@frivol.com''Frivol.com');
  72.         $mail->setBody($this->getTwigEnvironment()->render('emails/user-registration.html.twig'$params), 'text/html');
  73.         return $this->getMailService()->queueMessage($mailMailPriority::HIGHEST);
  74.     }
  75.     protected function getTopAmateurs(): array
  76.     {
  77.         $paginator $this->amateurLayer->getTopAmateurs(130truefalse);
  78.         $amateurs = [];
  79.         foreach ($paginator as $amateur) {
  80.             $amateurs[] = $amateur;
  81.         }
  82.         shuffle($amateurs);
  83.         return $amateurs;
  84.     }
  85.     public function onPreparePasswordChangeSendConfirmEmail(PreparePasswordChangeEvent $event): bool
  86.     {
  87.         $account $event->getAccount();
  88.         $confirmationUrl $this->router->generate('frivol_account_password_change_confirm', [
  89.             'confirmationToken' => $account->getConfirmationToken(),
  90.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  91.         $params = [
  92.             'confirmationUrl' => $confirmationUrl,
  93.         ];
  94.         $mail = new \Swift_Message('Passwort ändern bei Frivol.com');
  95.         $mail->addTo($account->getEmail());
  96.         $mail->setFrom('support@frivol.com''Frivol.com');
  97.         $mail->setBody($this->getTwigEnvironment()->render('emails/password-change.html.twig'$params), 'text/html');
  98.         return $this->getMailService()->queueMessage($mailMailPriority::HIGHEST);
  99.     }
  100.     public function onPrepareEmailChangeSendConfirmEmail(PrepareEmailChangeEvent $event): bool
  101.     {
  102.         $account $event->getAccount();
  103.         $confirmationUrl $this->router->generate('frivol_account_email_change_confirm', [
  104.             'confirmationToken' => $account->getConfirmationToken(),
  105.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  106.         $params = [
  107.             'confirmationUrl' => $confirmationUrl,
  108.         ];
  109.         $mail = new \Swift_Message('E-Mail Adresse ändern bei Frivol.com');
  110.         $mail->addTo($account->getNextEmail());
  111.         $mail->setFrom('support@frivol.com''Frivol.com');
  112.         $mail->setBody($this->getTwigEnvironment()->render('emails/email-change.html.twig'$params), 'text/html');
  113.         return $this->getMailService()->queueMessage($mailMailPriority::HIGHEST);
  114.     }
  115.     public function onPasswordChangedSendInfoEmail(PasswordChangedEvent $event): bool
  116.     {
  117.         $account $event->getAccount();
  118.         $mail = new \Swift_Message('Frivol.com Passwort wurde geändert');
  119.         $mail->addTo($account->getEmail());
  120.         $mail->setFrom('support@frivol.com''Frivol.com');
  121.         $mail->setBody($this->getTwigEnvironment()->render('emails/password-changed.html.twig', []), 'text/html');
  122.         return $this->getMailService()->queueMessage($mailMailPriority::HIGHEST);
  123.     }
  124.     public function onEmailChangedSendInfoEmail(EmailChangedEvent $event): bool
  125.     {
  126.         $account $event->getAccount();
  127.         $mail = new \Swift_Message('Frivol.com E-Mail Adresse wurde geändert');
  128.         $mail->addTo($account->getEmail());
  129.         $mail->setFrom('support@frivol.com''Frivol.com');
  130.         $mail->setBody($this->getTwigEnvironment()->render('emails/email-changed.html.twig', []), 'text/html');
  131.         return $this->getMailService()->queueMessage($mailMailPriority::NORMAL);
  132.     }
  133. }