src/Controller/SecurityController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\MediaRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. class SecurityController extends AbstractController
  10. {
  11.     private MediaRepository $mediaRepository;
  12.     public function __construct(MediaRepository $mediaRepository)
  13.     {
  14.         $this->mediaRepository $mediaRepository;
  15.     }
  16.     /**
  17.      * @Route("/connexion", name="app_login")
  18.      */
  19.     public function login(AuthenticationUtils $authenticationUtilsSessionInterface $session): Response
  20.     {
  21.         $error $authenticationUtils->getLastAuthenticationError();
  22.         // last username entered by the user
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         $medias $this->mediaRepository->findAll();
  25.         $mediasCount count($medias);
  26.         $currentDate date('Y-m-d');
  27.         $seed strtotime($currentDate);
  28.         $currentIndex $session->get($currentDate);
  29.         if ($currentIndex === null || $currentIndex >= $mediasCount || $currentIndex 0) {
  30.             if ($mediasCount 0) {
  31.                 mt_srand($seed);
  32.                 $currentIndex mt_rand(0$mediasCount 1);
  33.             } else {
  34.                 $currentIndex 0;
  35.             }
  36.             $session->set($currentDate$currentIndex);
  37.         }
  38.         if (!empty($medias)) {
  39.             $media $medias[$currentIndex] ?? null;
  40.             if ($media) {
  41.                 $data base64_encode(stream_get_contents($media->getData()));
  42.                 $mimeType $media->getMimeType();
  43.                 $imageSrc 'data:' $mimeType ';base64,' $data;
  44.             } else {
  45.                 $imageSrc '';
  46.             }
  47.         } else {
  48.             $imageSrc '';
  49.         }
  50.         return $this->render('/security/login.html.twig', [
  51.             'last_username' => $lastUsername,
  52.             'error' => $error,
  53.             'media' => $imageSrc,
  54.         ]);
  55.     }
  56. }