<?php
namespace App\EventSubscriber\User;
use App\Utils\Emails\UserEmail;
use App\Entity\User\User;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ChangePasswordSubscriber implements EventSubscriberInterface
{
private $router;
private $userEmail;
public function __construct(UrlGeneratorInterface $router, UserEmail $userEmail)
{
$this->router = $router;
$this->userEmail = $userEmail;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::CHANGE_PASSWORD_SUCCESS => array('onChangePasswordSuccess', -10)
);
}
public function onChangePasswordSuccess(FormEvent $event)
{
$request = $event->getRequest();
$redirect = $this->router->generate('fos_user_profile_show');
// Envoi mail de notification
$user = $event->getForm()->getData();
$this->userEmail->alerteModificationMotdepasse($user);
if ($request->isXmlHttpRequest()) {
$event->setResponse(new JsonResponse(array('success' => true)));
}
else {
$response = new RedirectResponse($redirect);
$event->setResponse($response);
}
}
}