vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/UserRegistrationListener.php line 40

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 Doctrine\Persistence\ObjectManager;
  13. use Sylius\Bundle\UserBundle\Security\UserLoginInterface;
  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 Webmozart\Assert\Assert;
  23. final class UserRegistrationListener
  24. {
  25.     public function __construct(
  26.         private ObjectManager $userManager,
  27.         private GeneratorInterface $tokenGenerator,
  28.         private EventDispatcherInterface $eventDispatcher,
  29.         private ChannelContextInterface $channelContext,
  30.         private UserLoginInterface $userLogin,
  31.         private string $firewallContextName,
  32.     ) {
  33.     }
  34.     public function handleUserVerification(GenericEvent $event): void
  35.     {
  36.         $customer $event->getSubject();
  37.         Assert::isInstanceOf($customerCustomerInterface::class);
  38.         $user $customer->getUser();
  39.         Assert::notNull($user);
  40.         /** @var ChannelInterface $channel */
  41.         $channel $this->channelContext->getChannel();
  42.         if (!$channel->isAccountVerificationRequired()) {
  43.             $this->enableAndLogin($user);
  44.             return;
  45.         }
  46.         $this->sendVerificationEmail($user);
  47.     }
  48.     private function sendVerificationEmail(ShopUserInterface $user): void
  49.     {
  50.         $token $this->tokenGenerator->generate();
  51.         $user->setEmailVerificationToken($token);
  52.         $this->userManager->persist($user);
  53.         $this->userManager->flush();
  54.         $this->eventDispatcher->dispatch(new GenericEvent($user), UserEvents::REQUEST_VERIFICATION_TOKEN);
  55.     }
  56.     private function enableAndLogin(ShopUserInterface $user): void
  57.     {
  58.         $user->setEnabled(true);
  59.         $this->userManager->persist($user);
  60.         $this->userManager->flush();
  61.         $this->userLogin->login($user$this->firewallContextName);
  62.     }
  63. }