feat: use DB awards instead of live Wikidata calls for hint generation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
thibaud-leclere
2026-04-01 14:29:36 +02:00
parent 0fd0b85b8f
commit fb13a8819d
2 changed files with 77 additions and 11 deletions

View File

@@ -11,7 +11,7 @@ use App\Entity\User;
use App\Repository\ActorRepository;
use App\Repository\MovieRepository;
use App\Repository\MovieRoleRepository;
use App\Gateway\WikidataGateway;
use App\Repository\AwardRepository;
use Doctrine\ORM\EntityManagerInterface;
class GameGridGenerator
@@ -20,7 +20,7 @@ class GameGridGenerator
private readonly ActorRepository $actorRepository,
private readonly MovieRoleRepository $movieRoleRepository,
private readonly MovieRepository $movieRepository,
private readonly WikidataGateway $wikidataGateway,
private readonly AwardRepository $awardRepository,
private readonly EntityManagerInterface $em,
) {}
@@ -173,16 +173,11 @@ class GameGridGenerator
return ['type' => 'character', 'data' => (string) $role->getId()];
case 'award':
try {
$awards = $this->wikidataGateway->getAwards($rowActor);
} catch (\Throwable) {
$award = $this->awardRepository->findOneRandomByActor($rowActor->getId());
if ($award === null) {
return null;
}
if (!empty($awards)) {
$award = $awards[array_rand($awards)];
return ['type' => 'award', 'data' => $award['name'] . ' (' . $award['year'] . ')'];
}
return null;
return ['type' => 'award', 'data' => (string) $award->getId()];
}
return null;
@@ -200,8 +195,23 @@ class GameGridGenerator
return match ($type) {
'film' => $this->movieRepository->find((int) $data)?->getTitle(),
'character' => $this->movieRoleRepository->find((int) $data)?->getCharacter(),
'award' => $data,
'award' => $this->resolveAwardHintText((int) $data),
default => null,
};
}
private function resolveAwardHintText(int $awardId): ?string
{
$award = $this->awardRepository->find($awardId);
if ($award === null) {
return null;
}
$text = $award->getName();
if ($award->getYear() !== null) {
$text .= ' (' . $award->getYear() . ')';
}
return $text;
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\Entity\Actor;
use App\Entity\Award;
use App\Entity\AwardType;
use App\Entity\GameRow;
use App\Repository\ActorRepository;
use App\Repository\AwardRepository;
use App\Repository\MovieRepository;
use App\Repository\MovieRoleRepository;
use App\Service\GameGridGenerator;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
class GameGridGeneratorTest extends TestCase
{
public function testResolveHintTextForAward(): void
{
$awardType = new AwardType();
$awardType->setName('Oscar')->setPattern('Academy Award');
$actor = new Actor();
$actor->setName('Test Actor');
$award = new Award();
$award->setName('Academy Award for Best Actor');
$award->setYear(2020);
$award->setActor($actor);
$award->setAwardType($awardType);
$awardRepository = $this->createMock(AwardRepository::class);
$awardRepository->method('find')->with(42)->willReturn($award);
$generator = new GameGridGenerator(
$this->createMock(ActorRepository::class),
$this->createMock(MovieRoleRepository::class),
$this->createMock(MovieRepository::class),
$awardRepository,
$this->createMock(EntityManagerInterface::class),
);
$row = new GameRow();
$row->setHintType('award');
$row->setHintData('42');
// Use reflection to test the private resolveHintText method
$method = new \ReflectionMethod($generator, 'resolveHintText');
$result = $method->invoke($generator, $row);
$this->assertSame('Academy Award for Best Actor (2020)', $result);
}
}