<?php
namespace Harmonizely\EventListener;
use FOS\UserBundle\Event\GetResponseNullableUserEvent;
use FOS\UserBundle\FOSUserEvents;
use Harmonizely\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ResettingListener implements EventSubscriberInterface
{
/**
* @var UrlGeneratorInterface
*/
private UrlGeneratorInterface $urlGenerator;
/**
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE => 'onResettingSendEmailInitialize',
];
}
/**
* @param GetResponseNullableUserEvent $event
* @return void
*/
public function onResettingSendEmailInitialize(GetResponseNullableUserEvent $event): void
{
$user = $event->getUser();
if ($user instanceof UserInterface) {
if ($user->isOnlySsoUser()) {
$event->setResponse(new RedirectResponse($this->urlGenerator->generate(
'fos_user_resetting_check_email',
[
'username' => $user->getEmail(),
]
)));
}
}
}
}