refactor: move WikidataAwardGateway to src/Gateway/WikidataGateway.php

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
thibaud-leclere
2026-03-31 11:14:20 +02:00
parent ecfc80c349
commit 6cbebb6367
2 changed files with 5 additions and 4 deletions

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Gateway;
use App\Entity\Actor;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class WikidataGateway
{
private const SPARQL_ENDPOINT = 'https://query.wikidata.org/sparql';
public function __construct(
private readonly HttpClientInterface $httpClient,
) {}
/**
* Fetch awards for an actor from Wikidata.
*
* @return list<array{name: string, year: int}>
*/
public function getAwards(Actor $actor): array
{
$sparql = $this->buildQuery($actor->getName());
$response = $this->httpClient->request('GET', self::SPARQL_ENDPOINT, [
'query' => [
'query' => $sparql,
'format' => 'json',
],
'headers' => [
'Accept' => 'application/sparql-results+json',
'User-Agent' => 'LtbxdActorle/1.0',
],
'timeout' => 5,
]);
$data = $response->toArray();
$awards = [];
foreach ($data['results']['bindings'] ?? [] as $binding) {
$name = $binding['awardLabel']['value'] ?? null;
$year = $binding['year']['value'] ?? null;
if ($name && $year) {
$awards[] = [
'name' => $name,
'year' => (int) substr($year, 0, 4),
];
}
}
return $awards;
}
private function buildQuery(string $actorName): string
{
$escaped = str_replace(['\\', '"', "\n", "\r"], ['\\\\', '\\"', '\\n', '\\r'], $actorName);
return <<<SPARQL
SELECT ?awardLabel ?year WHERE {
?person rdfs:label "{$escaped}"@en .
?person wdt:P31 wd:Q5 .
?person p:P166 ?awardStatement .
?awardStatement ps:P166 ?award .
?awardStatement pq:P585 ?date .
BIND(YEAR(?date) AS ?year)
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en" . }
}
ORDER BY DESC(?year)
SPARQL;
}
}