<?php
namespace App\EventListener;
use App\Controller\Backoffice\Freelancer\DashboardBackofficeController;
use App\Service\UtmManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* redirect freelancer who haven't completed their profile to the wizard
* Class UserConfigStepListener
* @package App\EventListener
*/
class UserConfigStepListener implements EventSubscriberInterface
{
private $router;
private $tokenStorage;
public function __construct(RouterInterface $router, TokenStorageInterface $tokenStorage )
{
$this->router = $router;
$this->tokenStorage = $tokenStorage;
}
public function onKernelController(ControllerEvent $event)
{
$request = $event->getRequest();
if (!$event->isMasterRequest()){
return;
}
//check user
if (!$token = $this->tokenStorage->getToken()) {
return ;
}
if (!$token->isAuthenticated()) {
return ;
}
if (!$user = $token->getUser()) {
return ;
}
//check controller
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
if (!$controller[0] instanceof DashboardBackofficeController) {
return;
}
//filter user
if($user->isTypeFreelancer() && !$user->hasFinishedConfigStep()){
$event->setController(function() {
return new RedirectResponse($this->router->generate('freelance_account_wizard_start'));
});
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}