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

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.     public function __construct(
  25.         private SenderInterface $emailSender,
  26.         private ChannelContextInterface $channelContext,
  27.         private LocaleContextInterface $localeContext,
  28.     ) {
  29.     }
  30.     public function sendResetPasswordTokenEmail(GenericEvent $event): void
  31.     {
  32.         $this->sendEmail($event->getSubject(), UserBundleEmails::RESET_PASSWORD_TOKEN);
  33.     }
  34.     public function sendResetPasswordPinEmail(GenericEvent $event): void
  35.     {
  36.         $this->sendEmail($event->getSubject(), UserBundleEmails::RESET_PASSWORD_PIN);
  37.     }
  38.     public function sendVerificationTokenEmail(GenericEvent $event): void
  39.     {
  40.         $this->sendEmail($event->getSubject(), UserBundleEmails::EMAIL_VERIFICATION_TOKEN);
  41.     }
  42.     public function sendUserRegistrationEmail(GenericEvent $event): void
  43.     {
  44.         $customer $event->getSubject();
  45.         Assert::isInstanceOf($customerCustomerInterface::class);
  46.         $user $customer->getUser();
  47.         if (null === $user) {
  48.             return;
  49.         }
  50.         $email $customer->getEmail();
  51.         if (empty($email)) {
  52.             return;
  53.         }
  54.         Assert::isInstanceOf($userShopUserInterface::class);
  55.         $this->sendEmail($userCoreBundleEmails::USER_REGISTRATION);
  56.     }
  57.     private function sendEmail(UserInterface $userstring $emailCode): void
  58.     {
  59.         $email $user->getEmail();
  60.         Assert::notNull($email);
  61.         $this->emailSender->send(
  62.             $emailCode,
  63.             [$email],
  64.             [
  65.                 'user' => $user,
  66.                 'channel' => $this->channelContext->getChannel(),
  67.                 'localeCode' => $this->localeContext->getLocaleCode(),
  68.             ],
  69.         );
  70.     }
  71. }