vendor/sulu/community-bundle/EventListener/MailListener.php line 83

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 Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
  12. use Sulu\Bundle\CommunityBundle\Event\AbstractCommunityEvent;
  13. use Sulu\Bundle\CommunityBundle\Event\UserCompletedEvent;
  14. use Sulu\Bundle\CommunityBundle\Event\UserConfirmedEvent;
  15. use Sulu\Bundle\CommunityBundle\Event\UserPasswordForgotEvent;
  16. use Sulu\Bundle\CommunityBundle\Event\UserPasswordResetedEvent;
  17. use Sulu\Bundle\CommunityBundle\Event\UserProfileSavedEvent;
  18. use Sulu\Bundle\CommunityBundle\Event\UserRegisteredEvent;
  19. use Sulu\Bundle\CommunityBundle\Mail\Mail;
  20. use Sulu\Bundle\CommunityBundle\Mail\MailFactoryInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. /**
  23.  * Send emails when specific events are thrown.
  24.  */
  25. class MailListener implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var MailFactoryInterface
  29.      */
  30.     private $mailFactory;
  31.     public function __construct(MailFactoryInterface $mailFactory)
  32.     {
  33.         $this->mailFactory $mailFactory;
  34.     }
  35.     /**
  36.      * @return array<string, mixed>
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             UserRegisteredEvent::class => ['sendRegistrationEmails'50],
  42.             UserConfirmedEvent::class => 'sendConfirmationEmails',
  43.             UserPasswordForgotEvent::class => 'sendPasswordForgetEmails',
  44.             UserPasswordResetedEvent::class => 'sendPasswordResetEmails',
  45.             UserCompletedEvent::class => 'sendCompletionEmails',
  46.             UserProfileSavedEvent::class => 'sendNotificationSaveProfile',
  47.         ];
  48.     }
  49.     /**
  50.      * Send registration emails.
  51.      */
  52.     public function sendRegistrationEmails(AbstractCommunityEvent $event): void
  53.     {
  54.         $this->sendTypeEmails($eventConfiguration::TYPE_REGISTRATION);
  55.     }
  56.     /**
  57.      * Send confirmation emails.
  58.      */
  59.     public function sendConfirmationEmails(AbstractCommunityEvent $event): void
  60.     {
  61.         $this->sendTypeEmails($eventConfiguration::TYPE_CONFIRMATION);
  62.     }
  63.     /**
  64.      * Send password forget emails.
  65.      */
  66.     public function sendPasswordForgetEmails(AbstractCommunityEvent $event): void
  67.     {
  68.         $this->sendTypeEmails($eventConfiguration::TYPE_PASSWORD_FORGET);
  69.     }
  70.     /**
  71.      * Send password reset emails.
  72.      */
  73.     public function sendPasswordResetEmails(AbstractCommunityEvent $event): void
  74.     {
  75.         $this->sendTypeEmails($eventConfiguration::TYPE_PASSWORD_RESET);
  76.     }
  77.     /**
  78.      * Send password reset emails.
  79.      */
  80.     public function sendCompletionEmails(AbstractCommunityEvent $event): void
  81.     {
  82.         $this->sendTypeEmails($eventConfiguration::TYPE_COMPLETION);
  83.     }
  84.     /**
  85.      * Send notification email for profile save.
  86.      */
  87.     public function sendNotificationSaveProfile(AbstractCommunityEvent $event): void
  88.     {
  89.         $this->sendTypeEmails($eventConfiguration::TYPE_PROFILE);
  90.     }
  91.     /**
  92.      * Send emails for specific type.
  93.      */
  94.     protected function sendTypeEmails(AbstractCommunityEvent $eventstring $type): void
  95.     {
  96.         $config $event->getConfig();
  97.         $mail Mail::create(
  98.             $config[Configuration::EMAIL_FROM],
  99.             $config[Configuration::EMAIL_TO],
  100.             $config[$type][Configuration::EMAIL]
  101.         );
  102.         $this->mailFactory->sendEmails($mail$event->getUser());
  103.     }
  104. }