vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/CustomerEmailUpdaterListener.php line 61

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\SectionResolver\SectionProviderInterface;
  13. use Sylius\Bundle\ShopBundle\SectionResolver\ShopSection;
  14. use Sylius\Bundle\UserBundle\UserEvents;
  15. use Sylius\Component\Channel\Context\ChannelContextInterface;
  16. use Sylius\Component\Core\Model\ChannelInterface;
  17. use Sylius\Component\Core\Model\CustomerInterface;
  18. use Sylius\Component\Core\Model\ShopUserInterface;
  19. use Sylius\Component\User\Security\Generator\GeneratorInterface;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\EventDispatcher\GenericEvent;
  22. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  23. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  24. use Webmozart\Assert\Assert;
  25. final class CustomerEmailUpdaterListener
  26. {
  27.     /** @var GeneratorInterface */
  28.     private $tokenGenerator;
  29.     /** @var ChannelContextInterface */
  30.     private $channelContext;
  31.     /** @var EventDispatcherInterface */
  32.     private $eventDispatcher;
  33.     /** @var SessionInterface */
  34.     private $session;
  35.     /** @var SectionProviderInterface */
  36.     private $uriBasedSectionContext;
  37.     public function __construct(
  38.         GeneratorInterface $tokenGenerator,
  39.         ChannelContextInterface $channelContext,
  40.         EventDispatcherInterface $eventDispatcher,
  41.         SessionInterface $session,
  42.         SectionProviderInterface $uriBasedSectionContext
  43.     ) {
  44.         $this->tokenGenerator $tokenGenerator;
  45.         $this->channelContext $channelContext;
  46.         $this->eventDispatcher $eventDispatcher;
  47.         $this->session $session;
  48.         $this->uriBasedSectionContext $uriBasedSectionContext;
  49.     }
  50.     public function eraseVerification(GenericEvent $event): void
  51.     {
  52.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  53.             return;
  54.         }
  55.         $customer $event->getSubject();
  56.         /** @var CustomerInterface $customer */
  57.         Assert::isInstanceOf($customerCustomerInterface::class);
  58.         /** @var ShopUserInterface|null $user */
  59.         $user $customer->getUser();
  60.         Assert::isInstanceOf($userShopUserInterface::class);
  61.         if ($customer->getEmail() !== $user->getUsername()) {
  62.             $user->setVerifiedAt(null);
  63.             /** @var ChannelInterface $channel */
  64.             $channel $this->channelContext->getChannel();
  65.             if ($channel->isAccountVerificationRequired()) {
  66.                 $token $this->tokenGenerator->generate();
  67.                 $user->setEmailVerificationToken($token);
  68.                 $user->setEnabled(false);
  69.             }
  70.         }
  71.     }
  72.     public function sendVerificationEmail(GenericEvent $event): void
  73.     {
  74.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  75.             return;
  76.         }
  77.         $customer $event->getSubject();
  78.         /** @var CustomerInterface $customer */
  79.         Assert::isInstanceOf($customerCustomerInterface::class);
  80.         /** @var ShopUserInterface $user */
  81.         $user $customer->getUser();
  82.         Assert::isInstanceOf($userShopUserInterface::class);
  83.         /** @var ChannelInterface $channel */
  84.         $channel $this->channelContext->getChannel();
  85.         if (!$channel->isAccountVerificationRequired()) {
  86.             return;
  87.         }
  88.         if (!$user->isEnabled() && !$user->isVerified() && null !== $user->getEmailVerificationToken()) {
  89.             $this->eventDispatcher->dispatch(new GenericEvent($user), UserEvents::REQUEST_VERIFICATION_TOKEN);
  90.             /** @var FlashBagInterface $flashBag */
  91.             $flashBag $this->session->getBag('flashes');
  92.             $flashBag->add('success''sylius.user.verify_email_request');
  93.         }
  94.     }
  95. }