src/EventListener/ResettingListener.php line 42

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\EventListener;
  3. use FOS\UserBundle\Event\GetResponseNullableUserEvent;
  4. use FOS\UserBundle\FOSUserEvents;
  5. use Harmonizely\Model\UserInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. class ResettingListener implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var UrlGeneratorInterface
  13.      */
  14.     private UrlGeneratorInterface $urlGenerator;
  15.     /**
  16.      * @param UrlGeneratorInterface $urlGenerator
  17.      */
  18.     public function __construct(UrlGeneratorInterface $urlGenerator)
  19.     {
  20.         $this->urlGenerator $urlGenerator;
  21.     }
  22.     /**
  23.      * {@inheritdoc}
  24.      */
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE => 'onResettingSendEmailInitialize',
  29.         ];
  30.     }
  31.     /**
  32.      * @param GetResponseNullableUserEvent $event
  33.      * @return void
  34.      */
  35.     public function onResettingSendEmailInitialize(GetResponseNullableUserEvent $event): void
  36.     {
  37.         $user $event->getUser();
  38.         if ($user instanceof UserInterface) {
  39.             if ($user->isOnlySsoUser()) {
  40.                 $event->setResponse(new RedirectResponse($this->urlGenerator->generate(
  41.                     'fos_user_resetting_check_email',
  42.                     [
  43.                         'username' => $user->getEmail(),
  44.                     ]
  45.                 )));
  46.             }
  47.         }
  48.     }
  49. }