<?php
namespace App\Controller;
use App\Repository\MediaRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
private MediaRepository $mediaRepository;
public function __construct(MediaRepository $mediaRepository)
{
$this->mediaRepository = $mediaRepository;
}
/**
* @Route("/connexion", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, SessionInterface $session): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$medias = $this->mediaRepository->findAll();
$mediasCount = count($medias);
$currentDate = date('Y-m-d');
$seed = strtotime($currentDate);
$currentIndex = $session->get($currentDate);
if ($currentIndex === null || $currentIndex >= $mediasCount || $currentIndex < 0) {
if ($mediasCount > 0) {
mt_srand($seed);
$currentIndex = mt_rand(0, $mediasCount - 1);
} else {
$currentIndex = 0;
}
$session->set($currentDate, $currentIndex);
}
if (!empty($medias)) {
$media = $medias[$currentIndex] ?? null;
if ($media) {
$data = base64_encode(stream_get_contents($media->getData()));
$mimeType = $media->getMimeType();
$imageSrc = 'data:' . $mimeType . ';base64,' . $data;
} else {
$imageSrc = '';
}
} else {
$imageSrc = '';
}
return $this->render('/security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'media' => $imageSrc,
]);
}
}