feat: endpoint FetchMangaChapters et tests
This commit is contained in:
parent
3dc0a0b406
commit
879b8fa2dc
@@ -0,0 +1,57 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user