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:
@@ -11,7 +11,7 @@ use App\Entity\User;
|
|||||||
use App\Repository\ActorRepository;
|
use App\Repository\ActorRepository;
|
||||||
use App\Repository\MovieRepository;
|
use App\Repository\MovieRepository;
|
||||||
use App\Repository\MovieRoleRepository;
|
use App\Repository\MovieRoleRepository;
|
||||||
use App\Gateway\WikidataGateway;
|
use App\Repository\AwardRepository;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
class GameGridGenerator
|
class GameGridGenerator
|
||||||
@@ -20,7 +20,7 @@ class GameGridGenerator
|
|||||||
private readonly ActorRepository $actorRepository,
|
private readonly ActorRepository $actorRepository,
|
||||||
private readonly MovieRoleRepository $movieRoleRepository,
|
private readonly MovieRoleRepository $movieRoleRepository,
|
||||||
private readonly MovieRepository $movieRepository,
|
private readonly MovieRepository $movieRepository,
|
||||||
private readonly WikidataGateway $wikidataGateway,
|
private readonly AwardRepository $awardRepository,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -173,16 +173,11 @@ class GameGridGenerator
|
|||||||
return ['type' => 'character', 'data' => (string) $role->getId()];
|
return ['type' => 'character', 'data' => (string) $role->getId()];
|
||||||
|
|
||||||
case 'award':
|
case 'award':
|
||||||
try {
|
$award = $this->awardRepository->findOneRandomByActor($rowActor->getId());
|
||||||
$awards = $this->wikidataGateway->getAwards($rowActor);
|
if ($award === null) {
|
||||||
} catch (\Throwable) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!empty($awards)) {
|
return ['type' => 'award', 'data' => (string) $award->getId()];
|
||||||
$award = $awards[array_rand($awards)];
|
|
||||||
return ['type' => 'award', 'data' => $award['name'] . ' (' . $award['year'] . ')'];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -200,8 +195,23 @@ class GameGridGenerator
|
|||||||
return match ($type) {
|
return match ($type) {
|
||||||
'film' => $this->movieRepository->find((int) $data)?->getTitle(),
|
'film' => $this->movieRepository->find((int) $data)?->getTitle(),
|
||||||
'character' => $this->movieRoleRepository->find((int) $data)?->getCharacter(),
|
'character' => $this->movieRoleRepository->find((int) $data)?->getCharacter(),
|
||||||
'award' => $data,
|
'award' => $this->resolveAwardHintText((int) $data),
|
||||||
default => null,
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
56
tests/Service/GameGridGeneratorTest.php
Normal file
56
tests/Service/GameGridGeneratorTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user