src/EventSubscriber/GameSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\AreaRequiredInterface;
  4. use App\Entity\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. class GameSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var TokenStorageInterface
  17.      */
  18.     private $tokenStorage;
  19.     /**
  20.      * @var RouterInterface
  21.      */
  22.     private $router;
  23.     /**
  24.      * @var KernelInterface
  25.      */
  26.     private $kernel;
  27.     public function __construct(TokenStorageInterface $tokenStorageRouterInterface $routerKernelInterface $kernel)
  28.     {
  29.         $this->tokenStorage $tokenStorage;
  30.         $this->router $router;
  31.         $this->kernel $kernel;
  32.     }
  33.     public function onKernelController(ControllerEvent $event)
  34.     {
  35.         $controller $event->getController();
  36.         // when a controller class defines multiple action methods, the controller
  37.         // is returned as [$controllerInstance, 'methodName']
  38.         if (is_array($controller)) {
  39.             $controller $controller[0];
  40.         }
  41.         // Make sure you can't access game controllers when the player does not have an active area
  42.         if ($controller instanceof AreaRequiredInterface) {
  43.             $token $this->tokenStorage->getToken();
  44.             $user null;
  45.             if ($token) {
  46.                 /** @var User $user */
  47.                 $user $token->getUser();
  48.             }
  49.             if (!($user instanceof User) || !$user->hasArea()) {
  50.                 $redirectUrl $this->router->generate('game_index');
  51.                 $event->setController(function () use ($redirectUrl) {
  52.                     return new RedirectResponse($redirectUrl);
  53.                 });
  54.             }
  55.             // Check if area is ticking and redirect to a holding page if it is
  56.             $lock $this->kernel->getProjectDir() . "/tickarealock_{$user->getArea()->getId()}.lock";
  57.             if (file_exists($lock)) {
  58.                 $ref '';
  59.                 if ($event->getRequest()->isMethod(Request::METHOD_GET)) {
  60.                     $ref $event->getRequest()->getRequestUri();
  61.                 }
  62.                 $redirectUrl $this->router->generate('game_tick_hold', ['ref' => $ref]);
  63.                 $event->setController(function () use ($redirectUrl) {
  64.                     return new RedirectResponse($redirectUrl);
  65.                 });
  66.             }
  67.         }
  68.     }
  69.     /**
  70.      * {@inheritDoc}
  71.      * @codeCoverageIgnore
  72.      */
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         return [
  76.             KernelEvents::CONTROLLER => 'onKernelController'
  77.         ];
  78.     }
  79. }