src/EventSubscriber/UserLeagacySubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\UserRepository;
  4. use Symfony\Component\Security\Core\Security;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class UserLeagacySubscriber implements EventSubscriberInterface
  9. {
  10.     private $userRepository;
  11.     private $security;
  12.     const ROUTESARRAY = ['home_index'];
  13.     public function __construct(UserRepository $userRepositorySecurity $security){
  14.         $this->userRepository $userRepository;
  15.         $this->security $security;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         // return the subscribed events, their methods and priorities
  20.         return [
  21.             KernelEvents::REQUEST => ['onRequest'],
  22.         ];
  23.     }
  24.     
  25.     public function onRequest(RequestEvent $event)
  26.     {
  27.         $request $event->getRequest();
  28.         
  29.         if ( in_array($request->attributes->get('_route'), self::ROUTESARRAY) ) {
  30.             
  31.             $session $request->getSession();
  32.             
  33.             /** @var User $user */
  34.             $user $this->security->getUser();
  35.             $userId $user->getId();
  36.             
  37.             $userLegacyIncluded $this->userRepository->userLegacyIncluded($userId); 
  38.             $userLegacyIncluded_array $this->extractId($userLegacyIncluded);
  39.             
  40.             $userLegacyExcluded $this->userRepository->userLegacyExcluded($userId);
  41.             $userLegacyExcluded_array $this->extractId($userLegacyExcluded);
  42.             $session->set('userLegacyWithUser'$userLegacyIncluded_array);
  43.             $session->set('userLegacyUserLess'$userLegacyExcluded_array);
  44.             
  45.         }
  46.     }
  47.     
  48.     private function extractId($result){
  49.         $array = [];
  50.         foreach ($result as $value) {
  51.             array_push($array$value['id']);
  52.         }
  53.         return $array;
  54.     }
  55. }