vendor/sulu/community-bundle/EventListener/EmailConfirmationListener.php line 76

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\CommunityBundle\EventListener;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
  13. use Sulu\Bundle\CommunityBundle\Entity\EmailConfirmationToken;
  14. use Sulu\Bundle\CommunityBundle\Entity\EmailConfirmationTokenRepository;
  15. use Sulu\Bundle\CommunityBundle\Event\UserProfileSavedEvent;
  16. use Sulu\Bundle\CommunityBundle\Mail\Mail;
  17. use Sulu\Bundle\CommunityBundle\Mail\MailFactoryInterface;
  18. use Sulu\Bundle\SecurityBundle\Entity\User;
  19. use Sulu\Bundle\SecurityBundle\Util\TokenGeneratorInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. /**
  22.  * Compares user-email and contact main-email.
  23.  * If they are different a confirmation link will be send.
  24.  */
  25. class EmailConfirmationListener implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var MailFactoryInterface
  29.      */
  30.     private $mailFactory;
  31.     /**
  32.      * @var EntityManagerInterface
  33.      */
  34.     private $entityManager;
  35.     /**
  36.      * @var TokenGeneratorInterface
  37.      */
  38.     private $tokenGenerator;
  39.     /**
  40.      * @var EmailConfirmationTokenRepository
  41.      */
  42.     private $emailConformationRepository;
  43.     public function __construct(
  44.         MailFactoryInterface $mailFactory,
  45.         EntityManagerInterface $entityManager,
  46.         EmailConfirmationTokenRepository $emailConformationRepository,
  47.         TokenGeneratorInterface $tokenGenerator
  48.     ) {
  49.         $this->mailFactory $mailFactory;
  50.         $this->entityManager $entityManager;
  51.         $this->emailConformationRepository $emailConformationRepository;
  52.         $this->tokenGenerator $tokenGenerator;
  53.     }
  54.     /**
  55.      * @return array<string, mixed>
  56.      */
  57.     public static function getSubscribedEvents()
  58.     {
  59.         return [
  60.             UserProfileSavedEvent::class => 'sendConfirmationOnEmailChange',
  61.         ];
  62.     }
  63.     /**
  64.      * Send confirmation-email if email-address has changed.
  65.      */
  66.     public function sendConfirmationOnEmailChange(UserProfileSavedEvent $event): void
  67.     {
  68.         $user $event->getUser();
  69.         if (!$user instanceof User) {
  70.             throw new \RuntimeException('Community bundle user need to be instance uf Sulu User');
  71.         }
  72.         if ($user->getEmail() === $user->getContact()->getMainEmail()) {
  73.             return;
  74.         }
  75.         $entity $this->emailConformationRepository->findByUser($user);
  76.         $token $this->tokenGenerator->generateToken();
  77.         if (!$entity instanceof EmailConfirmationToken) {
  78.             $entity = new EmailConfirmationToken($user);
  79.             $this->entityManager->persist($entity);
  80.         }
  81.         $entity->setToken($token);
  82.         $this->entityManager->flush();
  83.         $this->mailFactory->sendEmails(
  84.             Mail::create(
  85.                 $event->getConfigProperty(Configuration::EMAIL_FROM),
  86.                 $event->getConfigProperty(Configuration::EMAIL_TO),
  87.                 $event->getConfigTypeProperty(Configuration::TYPE_EMAIL_CONFIRMATIONConfiguration::EMAIL)
  88.             )->setUserEmail($user->getContact()->getMainEmail()),
  89.             $user,
  90.             ['token' => $entity->getToken()]
  91.         );
  92.     }
  93. }