src/Event/PqnaRoutesListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Service\SynchroHubService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. class PqnaRoutesListener implements EventSubscriberInterface
  11. {
  12.     private $urlGenerator;
  13.     private $security;
  14.     private $SynchroHubService;
  15.     public function __construct(
  16.         UrlGeneratorInterface $urlGenerator,
  17.         Security $security,
  18.         SynchroHubService $SynchroHubService
  19.     ) {
  20.         $this->urlGenerator $urlGenerator;
  21.         $this->security $security;
  22.         $this->SynchroHubService $SynchroHubService;
  23.     }
  24.     public function onKernelRequest(RequestEvent $event)
  25.     {
  26.         $request $event->getRequest();
  27.         $routeName $request->attributes->get('_route');
  28.         //On verifie si user et si sur la route du formulaire
  29.         if ($_POST && $routeName === 'sulu_community.profile' && $this->security->getUser()) {
  30.             $this->SynchroHubService->synchPqnaUserDatasToHub($_POST['profile']);
  31.             return; // On n'utilise pas les evenements de doctrine car ça ne fonctionne pas avec Sulu
  32.         }
  33.         if ($this->security->getUser() && $routeName === 'sulu_community.registration' || $this->security->getUser() && $routeName === 'sulu_community.login') {
  34.             $homepageUrl $this->urlGenerator->generate('sulu_community.profile'); //On redirige vers le profil
  35.             $response = new RedirectResponse($homepageUrl);
  36.             $event->setResponse($response);
  37.             return;
  38.         }
  39.         if ($this->security->getUser() && $routeName === 'sulu_community.profile') {
  40.             $this->SynchroHubService->syncOptinsFromHubToPqna($this->security->getUser()->getEmail());
  41.             return;
  42.         }
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::REQUEST => 'onKernelRequest',
  48.         ];
  49.     }
  50. }