58 lines
1.9 KiB
PHP
58 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,
|
|
false,
|
|
new \DateTimeImmutable()
|
|
);
|
|
|
|
$this->mangaRepository->saveChapter($chapter);
|
|
}
|
|
|
|
$offset += $limit;
|
|
$hasMore = count($feed['data']) === $limit;
|
|
}
|
|
}
|
|
}
|