<?php
namespace App\Event;
use App\Service\SynchroHubService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
class PqnaRoutesListener implements EventSubscriberInterface
{
private $urlGenerator;
private $security;
private $SynchroHubService;
public function __construct(
UrlGeneratorInterface $urlGenerator,
Security $security,
SynchroHubService $SynchroHubService
) {
$this->urlGenerator = $urlGenerator;
$this->security = $security;
$this->SynchroHubService = $SynchroHubService;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$routeName = $request->attributes->get('_route');
//On verifie si user et si sur la route du formulaire
if ($_POST && $routeName === 'sulu_community.profile' && $this->security->getUser()) {
$this->SynchroHubService->synchPqnaUserDatasToHub($_POST['profile']);
return; // On n'utilise pas les evenements de doctrine car ça ne fonctionne pas avec Sulu
}
if ($this->security->getUser() && $routeName === 'sulu_community.registration' || $this->security->getUser() && $routeName === 'sulu_community.login') {
$homepageUrl = $this->urlGenerator->generate('sulu_community.profile'); //On redirige vers le profil
$response = new RedirectResponse($homepageUrl);
$event->setResponse($response);
return;
}
if ($this->security->getUser() && $routeName === 'sulu_community.profile') {
$this->SynchroHubService->syncOptinsFromHubToPqna($this->security->getUser()->getEmail());
return;
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}