<?php
// src/EventListener/RequestListener.php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use App\Service\ConfigService;
class RewardListener
{
private $urlGenerator;
private $authorizationChecker;
private $security;
public function __construct(UrlGeneratorInterface $urlGenerator, AuthorizationCheckerInterface $authorizationChecker, Security $security, private ConfigService $configService)
{
$this->urlGenerator = $urlGenerator;
$this->authorizationChecker = $authorizationChecker;
$this->security = $security;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$user = $this->security->getUser();
$excludePaths = [$this->urlGenerator->generate('app_choose_reward'),$this->urlGenerator->generate('app_select_chosen_reward')];
foreach($excludePaths as $path){
if(strncmp($request->getPathInfo(), $path, strlen($path)) == 0){
return;
}
}
$url = $this->urlGenerator->generate('app_choose_reward');
if($user && $user->getWaitingUserRankingRewardChoice() && strncmp($request->getPathInfo(), $url, strlen($url)) != 0 ){
$event->setResponse(new RedirectResponse($url));
}
}
}