src/EventSubscriber/TwigEventSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use App\Repository\ChatRepository;
  6. use App\Repository\UserRepository;
  7. use App\Repository\SubjectRepository;
  8. use App\Repository\TheClassRepository;
  9. use Symfony\Component\Security\Core\Security;
  10. use Twig\Environment;
  11. class TwigEventSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private ChatRepository $chatRepo,
  15.         private Security $security,
  16.         private Environment $twig,
  17.         private UserRepository $userRepo,
  18.         private SubjectRepository $subjectRepo,
  19.         private TheClassRepository $classRepo
  20.     ){}
  21.     
  22.     public function onControllerEvent(ControllerEvent $event): void
  23.     {
  24.         $user $this->security->getUser();
  25.         
  26.         if ($user) {
  27.             $school $user->getSchool();
  28.             $profClasses $this->subjectRepo->findProfClasses($user->getId());
  29.             $this->twig->addGlobal('profClasses'$profClasses);
  30.             $this->twig->addGlobal('allChat'$this->chatRepo->findByUser($user->getId()));
  31.             $this->twig->addGlobal('chatList'$this->ShowListForAllowedChat());
  32.             $this->twig->addGlobal('allClasses'$this->classRepo->getClassByLevel());
  33.             $this->twig->addGlobal('all_classes'$this->classRepo->findBy(['school' => $school]));
  34.         }
  35.        // dd($this->chatRepo->findByUser($user->getId()));
  36.     }
  37.     public function ShowListForAllowedChat()
  38.     {
  39.         $user $this->security->getUser();
  40.         if ($user) {
  41.             $type $user->getUserType();
  42.             if ($type == 'prof') {
  43.                 return $list $this->userRepo->findByUserTypes('parent''admin'$user->getId());
  44.             } elseif ($type == 'admin') {
  45.             return $list $this->userRepo->findByNameAsc($user->getId());
  46.             }
  47.         }
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             ControllerEvent::class => 'onControllerEvent',
  53.         ];
  54.     }
  55. }