Files
Mangarr/src/Domain/Manga/Application/CommandHandler/FetchMangaChaptersHandler.php
ext.jeremy.guillot@maxicoffee.domains 879b8fa2dc feat: endpoint FetchMangaChapters et tests
2025-02-11 18:00:49 +01:00

57 lines
1.9 KiB
PHP

<?php
namespace App\Domain\Manga\Application\CommandHandler;
use App\Domain\Manga\Application\Command\FetchMangaChapters;
use App\Domain\Manga\Domain\Contract\Client\MangadexClientInterface;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Chapter;
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
use Ramsey\Uuid\Uuid;
readonly class FetchMangaChaptersHandler
{
public function __construct(
private MangadexClientInterface $mangadexClient,
private MangaRepositoryInterface $mangaRepository
) {}
public function handle(FetchMangaChapters $command): void
{
$manga = $this->mangaRepository->findByExternalId(new ExternalId($command->externalId));
if ($manga === null) {
throw new \RuntimeException('Manga not found');
}
$offset = 0;
$limit = 500;
$hasMore = true;
while ($hasMore) {
$feed = $this->mangadexClient->getMangaFeed(
$command->externalId,
$offset,
$limit
);
foreach ($feed['data'] as $chapterData) {
$chapter = new Chapter(
new ChapterId((string) Uuid::uuid4()),
$manga->getId()->getValue(),
(float) $chapterData['attributes']['chapter'],
$chapterData['attributes']['title'],
isset($chapterData['attributes']['volume']) ? (int) $chapterData['attributes']['volume'] : null,
true,
new \DateTimeImmutable()
);
$this->mangaRepository->saveChapter($chapter);
}
$offset += $limit;
$hasMore = count($feed['data']) === $limit;
}
}
}