From fb13a8819dfe4bf82991d22ba3907444e4e311b0 Mon Sep 17 00:00:00 2001 From: thibaud-leclere Date: Wed, 1 Apr 2026 14:29:36 +0200 Subject: [PATCH] feat: use DB awards instead of live Wikidata calls for hint generation Co-Authored-By: Claude Sonnet 4.6 --- src/Service/GameGridGenerator.php | 32 +++++++++----- tests/Service/GameGridGeneratorTest.php | 56 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 tests/Service/GameGridGeneratorTest.php diff --git a/src/Service/GameGridGenerator.php b/src/Service/GameGridGenerator.php index cb0db10..3d923a6 100644 --- a/src/Service/GameGridGenerator.php +++ b/src/Service/GameGridGenerator.php @@ -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; + } } diff --git a/tests/Service/GameGridGeneratorTest.php b/tests/Service/GameGridGeneratorTest.php new file mode 100644 index 0000000..11e364c --- /dev/null +++ b/tests/Service/GameGridGeneratorTest.php @@ -0,0 +1,56 @@ +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); + } +}