src/EventSubscriber/User/ChangePasswordSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\User;
  3.  
  4. use App\Utils\Emails\UserEmail;
  5. use App\Entity\User\User;
  6. use FOS\UserBundle\FOSUserEvents;
  7. use FOS\UserBundle\Event\FormEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. class ChangePasswordSubscriber implements EventSubscriberInterface
  13. {
  14.     private $router;
  15.     private $userEmail;
  16.  
  17.     public function __construct(UrlGeneratorInterface $routerUserEmail $userEmail)
  18.     {
  19.         $this->router     $router;
  20.         $this->userEmail  $userEmail;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return array(
  25.             FOSUserEvents::CHANGE_PASSWORD_SUCCESS => array('onChangePasswordSuccess', -10)
  26.         );
  27.     }
  28.  
  29.     public function onChangePasswordSuccess(FormEvent $event)
  30.     {
  31.         $request $event->getRequest();
  32.         
  33.         $redirect $this->router->generate('fos_user_profile_show');
  34.         
  35.         // Envoi mail de notification
  36.         $user $event->getForm()->getData();
  37.         
  38.         $this->userEmail->alerteModificationMotdepasse($user);
  39.         if ($request->isXmlHttpRequest()) {
  40.             $event->setResponse(new JsonResponse(array('success' => true)));
  41.         }
  42.         else {
  43.             $response = new RedirectResponse($redirect);
  44.             $event->setResponse($response);
  45.         }
  46.     }
  47. }