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

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.     public function __construct(
  28.         private GeneratorInterface $tokenGenerator,
  29.         private ChannelContextInterface $channelContext,
  30.         private EventDispatcherInterface $eventDispatcher,
  31.         private SessionInterface $session,
  32.         private SectionProviderInterface $uriBasedSectionContext,
  33.     ) {
  34.     }
  35.     public function eraseVerification(GenericEvent $event): void
  36.     {
  37.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  38.             return;
  39.         }
  40.         $customer $event->getSubject();
  41.         /** @var CustomerInterface $customer */
  42.         Assert::isInstanceOf($customerCustomerInterface::class);
  43.         /** @var ShopUserInterface|null $user */
  44.         $user $customer->getUser();
  45.         Assert::isInstanceOf($userShopUserInterface::class);
  46.         if ($customer->getEmail() !== $user->getUsername()) {
  47.             $user->setVerifiedAt(null);
  48.             /** @var ChannelInterface $channel */
  49.             $channel $this->channelContext->getChannel();
  50.             if ($channel->isAccountVerificationRequired()) {
  51.                 $token $this->tokenGenerator->generate();
  52.                 $user->setEmailVerificationToken($token);
  53.                 $user->setEnabled(false);
  54.             }
  55.         }
  56.     }
  57.     public function sendVerificationEmail(GenericEvent $event): void
  58.     {
  59.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  60.             return;
  61.         }
  62.         $customer $event->getSubject();
  63.         /** @var CustomerInterface $customer */
  64.         Assert::isInstanceOf($customerCustomerInterface::class);
  65.         /** @var ShopUserInterface $user */
  66.         $user $customer->getUser();
  67.         Assert::isInstanceOf($userShopUserInterface::class);
  68.         /** @var ChannelInterface $channel */
  69.         $channel $this->channelContext->getChannel();
  70.         if (!$channel->isAccountVerificationRequired()) {
  71.             return;
  72.         }
  73.         if (!$user->isEnabled() && !$user->isVerified() && null !== $user->getEmailVerificationToken()) {
  74.             $this->eventDispatcher->dispatch(new GenericEvent($user), UserEvents::REQUEST_VERIFICATION_TOKEN);
  75.             /** @var FlashBagInterface $flashBag */
  76.             $flashBag $this->session->getBag('flashes');
  77.             $flashBag->add('success''sylius.user.verify_email_request');
  78.         }
  79.     }
  80. }