feat(manga): implémenter la page Découvrir avec recommandations MangaDex

- Endpoint GET /api/manga-discover via DiscoverMangaStateProvider + DiscoverMangaHandler
- Algorithme : top 5 manga de la collection → appel /manga/{id}/recommendation
  par source → agrégation avec système de votes (multi-sources = plus pertinent)
- Filtrage : tags exclus (Oneshot, Doujinshi, Self-Published), contentRating,
  et suppression des manga déjà en bibliothèque
- Page Vue DiscoverPage.vue : chargement auto au montage, bouton Actualiser,
  modal détail, ajout à la bibliothèque
- Adapteurs InMemory de test mis à jour (discover + getMangaRecommendations)
This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2026-03-15 21:43:57 +01:00
parent 65453c87e5
commit 814fe46ce5
14 changed files with 482 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
<?php
namespace App\Domain\Manga\Application\Query;
readonly class DiscoverManga
{
}

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Domain\Manga\Application\QueryHandler;
use App\Domain\Manga\Application\Query\DiscoverManga;
use App\Domain\Manga\Application\Response\MangaSearchItem;
use App\Domain\Manga\Application\Response\MangaSearchResponse;
use App\Domain\Manga\Domain\Contract\Provider\MangaProviderInterface;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Manga;
readonly class DiscoverMangaHandler
{
public function __construct(
private MangaRepositoryInterface $mangaRepository,
private MangaProviderInterface $mangaProvider
) {
}
public function handle(DiscoverManga $query): MangaSearchResponse
{
$localMangas = $this->mangaRepository->findAll(page: 1, limit: 1000);
$ownedExternalIds = [];
$mangasWithRating = [];
foreach ($localMangas as $manga) {
if (!$manga->getExternalId()) {
continue;
}
$ownedExternalIds[] = $manga->getExternalId()->getValue();
$mangasWithRating[] = $manga;
}
usort($mangasWithRating, fn ($a, $b) => ($b->getRating() ?? 0) <=> ($a->getRating() ?? 0));
$sourceIds = array_map(
fn (Manga $m) => $m->getExternalId()->getValue(),
array_slice($mangasWithRating, 0, 5)
);
$collection = $this->mangaProvider->discover($sourceIds);
$recommendations = array_values(array_filter(
$collection->getItems(),
fn (Manga $m) => $m->getExternalId() === null
|| !in_array($m->getExternalId()->getValue(), $ownedExternalIds, true)
));
return new MangaSearchResponse(
array_map(
fn (Manga $manga, int $index) => new MangaSearchItem(
id: $index,
externalId: $manga->getExternalId()->getValue(),
title: $manga->getTitle()->getValue(),
slug: $manga->getSlug()->getValue(),
description: $manga->getDescription(),
author: $manga->getAuthor(),
publicationYear: $manga->getPublicationYear(),
genres: $manga->getGenres(),
status: $manga->getStatus(),
imageUrl: $manga->getImageUrl(),
thumbnailUrl: $manga->getImageUrls()?->getThumbnail(),
rating: $manga->getRating()
),
$recommendations,
array_keys($recommendations)
)
);
}
}

View File

@@ -93,4 +93,24 @@ interface MangadexClientInterface
* }
*/
public function getManga(string $mangaId): array;
/**
* @return array{
* data: array<array{
* id: string,
* attributes: array{
* title: array<string, string>,
* description: array<string, string>,
* year: ?int,
* status: string,
* tags: array<array{attributes: array{name: array<string, string>}}>
* },
* relationships: array<array{
* type: string,
* attributes: array{name: string|null, fileName: string|null}
* }>
* }>
* }
*/
public function getMangaRecommendations(string $mangaId): array;
}

View File

@@ -11,4 +11,9 @@ interface MangaProviderInterface
public function search(string $title): MangaCollection;
public function findByExternalId(ExternalId $externalId): ?Manga;
/**
* @param string[] $sourceExternalIds IDs MangaDex des manga sources
*/
public function discover(array $sourceExternalIds): MangaCollection;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaSearchCollection;
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider\DiscoverMangaStateProvider;
#[ApiResource(
shortName: 'Mangadex',
operations: [
new Get(
uriTemplate: '/manga-discover',
output: MangaSearchCollection::class,
provider: DiscoverMangaStateProvider::class
)
]
)]
class MangaDiscoverResource
{
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Domain\Manga\Application\Query\DiscoverManga;
use App\Domain\Manga\Application\QueryHandler\DiscoverMangaHandler;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaSearchCollection;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaSearchItem;
readonly class DiscoverMangaStateProvider implements ProviderInterface
{
public function __construct(private DiscoverMangaHandler $handler)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): MangaSearchCollection
{
$response = $this->handler->handle(new DiscoverManga());
return new MangaSearchCollection(
items: array_map(
fn ($item) => new MangaSearchItem(
externalId: $item->externalId,
title: $item->title,
slug: $item->slug,
description: $item->description,
author: $item->author,
publicationYear: $item->publicationYear,
genres: $item->genres,
status: $item->status,
imageUrl: $item->imageUrl,
thumbnailUrl: $item->thumbnailUrl,
rating: $item->rating
),
$response->items
)
);
}
}

View File

@@ -127,6 +127,35 @@ class MangadexClient implements MangadexClientInterface
]);
}
public function getMangaRecommendations(string $mangaId): array
{
// L'endpoint retourne des objets manga_recommendation avec des relationships
// vers les manga (sans détails). Il faut d'abord récupérer les IDs, puis
// fetcher les manga en batch avec leurs détails complets.
$recommendations = $this->get('/manga/' . $mangaId . '/recommendation');
$recommendedIds = [];
foreach ($recommendations['data'] ?? [] as $item) {
foreach ($item['relationships'] ?? [] as $rel) {
if ($rel['type'] === 'manga' && $rel['id'] !== $mangaId) {
$recommendedIds[] = $rel['id'];
}
}
}
if (empty($recommendedIds)) {
return ['data' => []];
}
return $this->get('/manga', [
'ids' => $recommendedIds,
'includes' => ['cover_art', 'author'],
'contentRating' => ['safe', 'suggestive', 'erotica'],
'excludedTags' => self::EXCLUDED_TAGS,
'limit' => count($recommendedIds),
]);
}
private function get(string $endpoint, array $params = []): array
{
try {

View File

@@ -135,6 +135,55 @@ readonly class MangadexProvider implements MangaProviderInterface
}
}
public function discover(array $sourceExternalIds): MangaCollection
{
if (empty($sourceExternalIds)) {
return new MangaCollection([]);
}
// Compter les votes : un manga recommandé par plusieurs sources est plus pertinent.
// On conserve aussi la position d'apparition pour départager les ex-aequo.
$votes = [];
$firstPosition = [];
$resultsById = [];
$position = 0;
foreach ($sourceExternalIds as $externalId) {
try {
$response = $this->client->getMangaRecommendations($externalId);
foreach ($response['data'] ?? [] as $result) {
$id = $result['id'];
$votes[$id] = ($votes[$id] ?? 0) + 1;
if (!isset($firstPosition[$id])) {
$firstPosition[$id] = $position++;
$resultsById[$id] = $result;
}
}
} catch (\Exception) {
continue;
}
}
if (empty($resultsById)) {
return new MangaCollection([]);
}
// Trier : votes décroissants (multi-sources = plus pertinent), puis position croissante (score API)
uksort($resultsById, function (string $a, string $b) use ($votes, $firstPosition): int {
$voteDiff = $votes[$b] - $votes[$a];
if ($voteDiff !== 0) {
return $voteDiff;
}
return $firstPosition[$a] <=> $firstPosition[$b];
});
$mangas = $this->createMangasFromResults(array_values($resultsById));
$this->enrichWithRatings($mangas);
return new MangaCollection($mangas);
}
public function findByExternalId(ExternalId $externalId): ?Manga
{
try {