feat: firsts unit tests for ScrapeChapterHandler.php

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-03 10:38:53 +01:00
parent 21fcdd1084
commit 89570ad951
31 changed files with 1105 additions and 291 deletions

View File

@@ -1,31 +1,40 @@
<?php
namespace App\Domain\Scraping\Application\Command\ScrapeChapter;
namespace App\Domain\Scraping\Application\CommandHandler;
use App\Domain\Scraping\Domain\Contract\ScraperInterface;
use App\Domain\Scraping\Domain\Repository\ScrapingJobRepositoryInterface;
use App\Domain\Scraping\Application\Command\ScrapeChapter;
use App\Domain\Scraping\Domain\Contract\Repository\ScrapingJobRepositoryInterface;
use App\Domain\Scraping\Domain\Contract\Service\ScraperInterface;
use App\Domain\Scraping\Domain\Event\ChapterScrapingFailed;
use App\Domain\Scraping\Domain\Event\ChapterScrapingStarted;
use Symfony\Component\Messenger\MessageBusInterface;
class ScrapeChapterHandler
readonly class ScrapeChapterHandler
{
public function __construct(
private readonly ScraperInterface $scraper,
private readonly ScrapingJobRepositoryInterface $scrapingJobRepository,
private readonly MessageBusInterface $eventBus
) {}
public function handle(ScrapeChapterCommand $command): void
{
$job = $this->scraper->createScrapingJob(
$command->chapterId,
$command->sourceId
);
$this->scrapingJobRepository->save($job);
$this->eventBus->dispatch(new ChapterScrapingStarted($job->getId()));
$this->scraper->scrape($job);
private ScraperInterface $scraper,
private ScrapingJobRepositoryInterface $scrapingJobRepository,
private MessageBusInterface $eventBus
) {
}
}
public function handle(ScrapeChapter $command): void
{
try {
$job = $this->scraper->createScrapingJob(
$command->mangaId,
$command->chapterId,
$command->sourceId,
);
$this->scrapingJobRepository->save($job);
$this->eventBus->dispatch(new ChapterScrapingStarted($job->getId()));
$this->scraper->scrape($job);
} catch (\Exception $e) {
$this->eventBus->dispatch(new ChapterScrapingFailed($command->chapterId, $e->getMessage()));
throw $e;
}
}
}