src/Sylius/Bundle/ShopBundle/EventListener/UserMailerListener.php line 42

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\ShopBundle\EventListener;
  12. use Sylius\Bundle\CoreBundle\Mailer\Emails;
  13. use Sylius\Bundle\UserBundle\EventListener\MailerListener;
  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\Mailer\Sender\SenderInterface;
  18. use Sylius\Component\Resource\Exception\UnexpectedTypeException;
  19. use Sylius\Component\User\Model\UserInterface;
  20. use Symfony\Component\EventDispatcher\GenericEvent;
  21. use Webmozart\Assert\Assert;
  22. final class UserMailerListener extends MailerListener
  23. {
  24.     /** @var ChannelContextInterface */
  25.     private $channelContext;
  26.     public function __construct(SenderInterface $emailSenderChannelContextInterface $channelContext)
  27.     {
  28.         parent::__construct($emailSender);
  29.         $this->channelContext $channelContext;
  30.     }
  31.     /**
  32.      * @throws UnexpectedTypeException
  33.      */
  34.     public function sendUserRegistrationEmail(GenericEvent $event): void
  35.     {
  36.         $customer $event->getSubject();
  37.         Assert::isInstanceOf($customerCustomerInterface::class);
  38.         $user $customer->getUser();
  39.         if (null === $user) {
  40.             return;
  41.         }
  42.         $email $customer->getEmail();
  43.         if (empty($email)) {
  44.             return;
  45.         }
  46.         Assert::isInstanceOf($userShopUserInterface::class);
  47.         $this->sendEmail($userEmails::USER_REGISTRATION);
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     protected function sendEmail(UserInterface $userstring $emailCode): void
  53.     {
  54.         $this->emailSender->send($emailCode, [$user->getEmail()], [
  55.             'user' => $user,
  56.             'channel' => $this->channelContext->getChannel(),
  57.         ]);
  58.     }
  59. }