src/AppBundle/Controller/DefaultController.php line 81

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace AppBundle\Controller;
  4. use AppBundle\Entity\Instance;
  5. use AppBundle\Entity\InstanceUser;
  6. use AppBundle\Entity\Language;
  7. use AppBundle\Entity\User;
  8. use CodersLab\Lms\SharedKernel\Application\Command\UpdateUserLanguage;
  9. use CodersLab\Lms\SharedKernel\Application\SecurityContext;
  10. use CodersLab\Lms\SharedKernel\Domain\Bus\CommandBus;
  11. use CodersLab\Lms\SharedKernel\Domain\IdentityAccess\UserLanguage;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. class DefaultController extends AbstractController
  19. {
  20.     protected ContainerBagInterface $params;
  21.     private CommandBus $commandBus;
  22.     private SecurityContext $securityContext;
  23.     public function __construct(ContainerBagInterface $paramsCommandBus $commandBusSecurityContext $securityContext)
  24.     {
  25.         $this->params $params;
  26.         $this->commandBus $commandBus;
  27.         $this->securityContext $securityContext;
  28.     }
  29.     /**
  30.      * @Route("/admin", name="homepage")
  31.      * @param SessionInterface $session
  32.      * @return Response
  33.      */
  34.     public function indexAction(): Response
  35.     {
  36.         $user $this->getUser();
  37.         $showGitHubConnectionMessage false;
  38.         
  39.         if ($user instanceof User) {
  40.             $instance null;
  41.             if ($instance instanceof Instance) {//jak dla mnie ten if siÄ™ nigdy nie wykona ;]
  42.                 $language $instance->getLanguage();
  43.                 if ($language instanceof Language) {
  44.                     return $this->redirectToRoute(
  45.                         'language',
  46.                         ['id' => $language->getId(), 'instance' => $instance->getId()]
  47.                     );
  48.                 }
  49.             }
  50.             $userInstances $user->getParticipations();
  51.             /** @var InstanceUser $instance */
  52.             foreach ($userInstances as $instance) {
  53.                 $language $instance->getInstance()->getLanguage();
  54.                 if ($language->isGithubRequired()) {
  55.                     $showGitHubConnectionMessage true;
  56.                     break;
  57.                 }
  58.             }
  59.         }
  60.         return $this->render('default/index.html.twig', [
  61.             'showGitHubConnectionMessage' => $user->getGithub() === null && $showGitHubConnectionMessage,
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/set_lang/{lang}", name="set_lang")
  66.      * @param string $lang
  67.      *
  68.      * @return Response
  69.      */
  70.     public function setLangAction(Request $requeststring $lang)
  71.     {
  72.         if ($this->securityContext->isUserLogged()) {
  73.             $this->commandBus->dispatch(new UpdateUserLanguage(
  74.                 (string)$this->securityContext->getLoggedUserId(),
  75.                 new UserLanguage($lang)
  76.             ));
  77.         }
  78.         $request->getSession()->set('_locale'$lang);
  79.         $request->setLocale($lang);
  80.         $redirectUrl $request->headers->get('referer');
  81.         
  82.         return $redirectUrl $this->redirect($request->headers->get('referer')) : new Response();
  83.     }
  84. }