vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/EventListener/MailerListener.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\EventListener;
  12. use Sylius\Bundle\CoreBundle\Mailer\Emails as CoreBundleEmails;
  13. use Sylius\Bundle\UserBundle\Mailer\Emails as UserBundleEmails;
  14. use Sylius\Component\Channel\Context\ChannelContextInterface;
  15. use Sylius\Component\Core\Model\CustomerInterface;
  16. use Sylius\Component\Core\Model\ShopUserInterface;
  17. use Sylius\Component\Locale\Context\LocaleContextInterface;
  18. use Sylius\Component\Mailer\Sender\SenderInterface;
  19. use Sylius\Component\User\Model\UserInterface;
  20. use Symfony\Component\EventDispatcher\GenericEvent;
  21. use Webmozart\Assert\Assert;
  22. final class MailerListener
  23. {
  24.     private SenderInterface $emailSender;
  25.     private ChannelContextInterface $channelContext;
  26.     private LocaleContextInterface $localeContext;
  27.     public function __construct(
  28.         SenderInterface $emailSender,
  29.         ChannelContextInterface $channelContext,
  30.         LocaleContextInterface $localeContext
  31.     ) {
  32.         $this->emailSender $emailSender;
  33.         $this->channelContext $channelContext;
  34.         $this->localeContext $localeContext;
  35.     }
  36.     public function sendResetPasswordTokenEmail(GenericEvent $event): void
  37.     {
  38.         $this->sendEmail($event->getSubject(), UserBundleEmails::RESET_PASSWORD_TOKEN);
  39.     }
  40.     public function sendResetPasswordPinEmail(GenericEvent $event): void
  41.     {
  42.         $this->sendEmail($event->getSubject(), UserBundleEmails::RESET_PASSWORD_PIN);
  43.     }
  44.     public function sendVerificationTokenEmail(GenericEvent $event): void
  45.     {
  46.         $this->sendEmail($event->getSubject(), UserBundleEmails::EMAIL_VERIFICATION_TOKEN);
  47.     }
  48.     public function sendUserRegistrationEmail(GenericEvent $event): void
  49.     {
  50.         $customer $event->getSubject();
  51.         Assert::isInstanceOf($customerCustomerInterface::class);
  52.         $user $customer->getUser();
  53.         if (null === $user) {
  54.             return;
  55.         }
  56.         $email $customer->getEmail();
  57.         if (empty($email)) {
  58.             return;
  59.         }
  60.         Assert::isInstanceOf($userShopUserInterface::class);
  61.         $this->sendEmail($userCoreBundleEmails::USER_REGISTRATION);
  62.     }
  63.     private function sendEmail(UserInterface $userstring $emailCode): void
  64.     {
  65.         $email $user->getEmail();
  66.         Assert::notNull($email);
  67.         $this->emailSender->send(
  68.             $emailCode,
  69.             [$email],
  70.             [
  71.                 'user' => $user,
  72.                 'channel' => $this->channelContext->getChannel(),
  73.                 'localeCode' => $this->localeContext->getLocaleCode(),
  74.             ]
  75.         );
  76.     }
  77. }