feat: endpoint FetchMangaChapters et tests
This commit is contained in:
parent
3dc0a0b406
commit
879b8fa2dc
@@ -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 = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user