Files
ltbxd-actorle/src/Controller/HomepageController.php
thibaud-leclere a196fac6c6 Generate grid
2026-01-31 16:17:24 +01:00

75 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller;
use App\Gateway\TMDBGateway;
use App\Repository\ActorRepository;
use App\Repository\MovieRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
class HomepageController extends AbstractController
{
public function __construct(
private readonly ActorRepository $actorRepository
) {}
#[Route('/')]
public function index(SerializerInterface $serializer): Response
{
// Final actor to be guessed
$mainActor = $this->actorRepository->findOneRandom(4);
// Actors for the grid
$actors = [];
$leftSize = 0;
$rightSize = 0;
foreach (str_split(strtolower($mainActor->getName())) as $char) {
if (!preg_match('/[a-z]/', $char)) {
continue;
}
$tryFindActor = 0;
do {
$actor = $this->actorRepository->findOneRandom(4, $char);
++$tryFindActor;
} while (
$actor === $mainActor
|| in_array($actor, array_map(fn ($actorMap) => $actorMap['actor'], $actors))
|| $tryFindActor < 5
);
$actorData = [
'actor' => $actor,
'pos' => strpos($actor->getName(), $char),
];
if ($leftSize < $actorData['pos']) {
$leftSize = $actorData['pos'];
}
$rightSizeActor = strlen($actor->getName()) - $actorData['pos'] - 1;
if ($rightSize < $rightSizeActor) {
$rightSize = $rightSizeActor;
}
$actors[] = $actorData;
}
// Predict grid size
$width = $rightSize + $leftSize + 1;
$middle = $leftSize;
return $this->render('homepage/index.html.twig', [
'mainActor' => $mainActor,
'actors' => $actors,
'width' => $width,
'middle' => $middle,
]);
}
}