src/Sylius/Bundle/ShopBundle/EventListener/UserRegistrationListener.php line 67

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\Common\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.     /** @var ObjectManager */
  26.     private $userManager;
  27.     /** @var GeneratorInterface */
  28.     private $tokenGenerator;
  29.     /** @var EventDispatcherInterface */
  30.     private $eventDispatcher;
  31.     /** @var ChannelContextInterface */
  32.     private $channelContext;
  33.     /** @var UserLoginInterface */
  34.     private $userLogin;
  35.     /** @var string */
  36.     private $firewallContextName;
  37.     /**
  38.      * @param string $firewallContextName
  39.      */
  40.     public function __construct(
  41.         ObjectManager $userManager,
  42.         GeneratorInterface $tokenGenerator,
  43.         EventDispatcherInterface $eventDispatcher,
  44.         ChannelContextInterface $channelContext,
  45.         UserLoginInterface $userLogin,
  46.         $firewallContextName
  47.     ) {
  48.         $this->userManager $userManager;
  49.         $this->tokenGenerator $tokenGenerator;
  50.         $this->eventDispatcher $eventDispatcher;
  51.         $this->channelContext $channelContext;
  52.         $this->userLogin $userLogin;
  53.         $this->firewallContextName $firewallContextName;
  54.     }
  55.     public function handleUserVerification(GenericEvent $event): void
  56.     {
  57.         $customer $event->getSubject();
  58.         Assert::isInstanceOf($customerCustomerInterface::class);
  59.         $user $customer->getUser();
  60.         Assert::notNull($user);
  61.         /** @var ChannelInterface $channel */
  62.         $channel $this->channelContext->getChannel();
  63.         if (!$channel->isAccountVerificationRequired()) {
  64.             $this->enableAndLogin($user);
  65.             return;
  66.         }
  67.         $this->sendVerificationEmail($user);
  68.     }
  69.     private function sendVerificationEmail(ShopUserInterface $user): void
  70.     {
  71.         $token $this->tokenGenerator->generate();
  72.         $user->setEmailVerificationToken($token);
  73.         $this->userManager->persist($user);
  74.         $this->userManager->flush();
  75.         $this->eventDispatcher->dispatch(UserEvents::REQUEST_VERIFICATION_TOKEN, new GenericEvent($user));
  76.     }
  77.     private function enableAndLogin(ShopUserInterface $user): void
  78.     {
  79.         $user->setEnabled(true);
  80.         $this->userManager->persist($user);
  81.         $this->userManager->flush();
  82.         $this->userLogin->login($user$this->firewallContextName);
  83.     }
  84. }