src/Core/EventSubscriber/RedirectLoggedUserSubscriber.php line 54
<?php/*** Copyright (c) 2019 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2019 TECLA Consulting Group oü*/namespace App\Core\EventSubscriber;use App\Core\Model\UserInterface;use JetBrains\PhpStorm\ArrayShape;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\RouterInterface;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;class RedirectLoggedUserSubscriber implements EventSubscriberInterface{/*** @var TokenStorageInterface*/private $tokenStorage;/*** @var RouterInterface*/private $router;public function __construct(TokenStorageInterface $tokenStorage, RouterInterface $router){$this->tokenStorage = $tokenStorage;$this->router = $router;}#[ArrayShape([KernelEvents::REQUEST => 'string'])]public static function getSubscribedEvents(): array{return [KernelEvents::REQUEST => 'onKernelRequest',];}public function onKernelRequest(RequestEvent $event){if (true === $this->isUserLogged() && true === $event->isMainRequest()) {$currentRoute = $event->getRequest()->attributes->get('_route');if (true === $this->isAuthenticatedUserOnAnonymousPage($currentRoute)) {$response = new RedirectResponse($this->router->generate('app_homepage'));$event->setResponse($response);}}}private function isUserLogged(){$token = $this->tokenStorage->getToken();if (null === $token) {return false;}$user = $token->getUser();return $user instanceof UserInterface;}private function isAuthenticatedUserOnAnonymousPage($currentRoute){return \in_array($currentRoute,['app_login','app_register','app_register_confirm','app.core.http.registration.email_sent','app.core.http.registration.token_expired','app_forgotten_password','app.core.http.forgotten_password.email_sent','app.core.http.forgotten_password.changed','app_forgotten_password_reset','app_forgotten_password_token_expired',]);}}