feat: endpoint FetchMangaChapters et tests

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-11 18:00:49 +01:00
parent 3dc0a0b406
commit 879b8fa2dc
20 changed files with 424 additions and 45 deletions

View File

@@ -6,18 +6,22 @@ use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\Manga;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
class InMemoryMangaRepository implements MangaRepositoryInterface
{
/** @var Manga[] */
/** @var array<string, Manga> */
private array $mangas = [];
/** @var array<string, Chapter[]> */
/** @var array<string, array<Chapter>> */
private array $chapters = [];
/** @var array<Chapter> */
private array $savedChapters = [];
public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array
{
$sortedMangas = $this->mangas;
$sortedMangas = array_values($this->mangas);
usort($sortedMangas, function (Manga $a, Manga $b) use ($sortBy, $sortOrder) {
$valueA = $this->getPropertyValue($a, $sortBy);
@@ -40,26 +44,17 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
public function findById(string $id): ?Manga
{
foreach ($this->mangas as $manga) {
if ($manga->getId()->getValue() === $id) {
return $manga;
}
}
return null;
return $this->mangas[$id] ?? null;
}
public function save(Manga $manga): void
{
$this->mangas[] = $manga;
$this->mangas[$manga->getId()->getValue()] = $manga;
}
public function delete(Manga $manga): void
{
$this->mangas = array_filter(
$this->mangas,
fn(Manga $existingManga) => !$existingManga->getId()->equals($manga->getId())
);
unset($this->mangas[$manga->getId()->getValue()]);
}
private function getPropertyValue(Manga $manga, string $property): mixed
@@ -91,7 +86,7 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
public function countChapters(string $mangaId): int
{
return isset($this->chapters[$mangaId]) ? count($this->chapters[$mangaId]) : 0;
return count($this->chapters[$mangaId] ?? []);
}
public function addChaptersToManga(string $mangaId, int $count): void
@@ -111,9 +106,35 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
}
}
public function findByExternalId(ExternalId $externalId): ?Manga
{
foreach ($this->mangas as $manga) {
if ($manga->getExternalId() && $manga->getExternalId()->getValue() === $externalId->getValue()) {
return $manga;
}
}
return null;
}
public function saveChapter(Chapter $chapter): void
{
$this->savedChapters[] = $chapter;
if (!isset($this->chapters[$chapter->getMangaId()])) {
$this->chapters[$chapter->getMangaId()] = [];
}
$this->chapters[$chapter->getMangaId()][] = $chapter;
}
/** @return array<Chapter> */
public function getSavedChapters(): array
{
return $this->savedChapters;
}
public function clear(): void
{
$this->mangas = [];
$this->chapters = [];
$this->savedChapters = [];
}
}

View File

@@ -9,6 +9,7 @@ class InMemoryMangadexClient implements MangadexClientInterface
private array $mangas = [];
private array $feeds = [];
private array $aggregates = [];
private array $mangaFeeds = [];
public function __construct(
array $mangas = [],
@@ -61,22 +62,7 @@ class InMemoryMangadexClient implements MangadexClientInterface
public function getMangaFeed(string $mangaId, int $offset = 0, int $limit = 500, string $order = 'asc'): array
{
if (!isset($this->feeds[$mangaId])) {
return [
'data' => [],
'total' => 0
];
}
$feed = $this->feeds[$mangaId];
if ($order === 'desc') {
$feed = array_reverse($feed);
}
return [
'data' => array_slice($feed, $offset, $limit),
'total' => count($feed)
];
return $this->mangaFeeds[$mangaId] ?? ['data' => []];
}
public function getMangaAggregate(string $mangaId): array
@@ -120,4 +106,9 @@ class InMemoryMangadexClient implements MangadexClientInterface
{
$this->aggregates[$mangaId] = $aggregate;
}
public function setMangaFeed(string $mangaId, array $feed): void
{
$this->mangaFeeds[$mangaId] = $feed;
}
}