feat: refactorisation de la gestion du scraping des chapitres en remplaçant les identifiants de manga et de chapitre par un identifiant de chapitre unique, amélioration de la récupération des sources préférées et ajout de la gestion des erreurs pour les échecs de scraping.

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-04-03 16:34:30 +02:00
parent e29433bb0c
commit c9f1771522
15 changed files with 270 additions and 104 deletions

View File

@@ -8,6 +8,12 @@ use App\Domain\Scraping\Domain\Model\Chapter;
class InMemoryChapterRepository implements ChapterRepositoryInterface
{
private array $chapters = [];
public function getById(string $id): ?Chapter
{
return $this->chapters[$id] ?? null;
}
public function getByMangaIdAndChapterNumber(string $mangaId, int $chapterNumber): Chapter
{
foreach ($this->chapters as $chapter) {

View File

@@ -16,19 +16,27 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
'test-manga',
'Test Manga',
'test-manga',
'2024',
'A test manga description',
'Test Author',
'A test manga description'
'2024',
[] // Pas de sources préférées par défaut
);
// Ajoute un manga avec des sources préférées pour les tests
$this->mangas['test-manga-with-sources'] = new Manga(
'test-manga-with-sources',
'Test Manga With Sources',
'test-manga-with-sources',
'A test manga with preferred sources',
'Test Author',
'2024',
['test-source'] // Une source préférée
);
}
public function getById(string $id): Manga
public function getById(string $id): ?Manga
{
if (!isset($this->mangas[$id])) {
throw new \RuntimeException('Manga not found');
}
return $this->mangas[$id];
return $this->mangas[$id] ?? null;
}
public function save(Manga $manga): void

View File

@@ -22,7 +22,9 @@ class InMemorySourceRepository implements SourceRepositoryInterface
[
'imageSelector' => 'img.manga-image',
'nextPageSelector' => null,
'chapterUrlFormat' => 'https://example.com/manga/{slug}/chapter-{chapterNumber}'
'chapterUrlFormat' => 'https://example.com/manga/{slug}/chapter-{chapterNumber}',
'scrapingType' => 'html',
'chapterSelector' => '.chapter-item'
],
true,
new DateTimeImmutable(),
@@ -30,13 +32,17 @@ class InMemorySourceRepository implements SourceRepositoryInterface
);
}
public function getById(string $id): Source
public function getById(string $id): ?Source
{
if (!isset($this->sources[$id])) {
throw new \RuntimeException('Source not found');
}
return $this->sources[$id] ?? null;
}
return $this->sources[$id];
/**
* @return Source[]
*/
public function getAll(): array
{
return array_values($this->sources);
}
public function save(Source $source): void

View File

@@ -74,9 +74,7 @@ class ScrapeChapterHandlerTest extends TestCase
public function testHandleSuccessfully(): void
{
$command = new ScrapeChapter(
mangaId: 'test-manga',
chapterNumber: '2',
sourceId: 'test-source'
chapterId: '1'
);
$this->handler->handle($command);
@@ -90,16 +88,14 @@ class ScrapeChapterHandlerTest extends TestCase
$this->assertInstanceOf(ChapterScraped::class, $dispatchedMessages[0]);
$this->assertEquals($job->id, $dispatchedMessages[0]->getJobId());
$chapter = $this->chapterRepository->getByMangaIdAndChapterNumber('test-manga', 2);
$chapter = $this->chapterRepository->getById('1');
$this->assertNotNull($chapter->cbzPath);
}
public function testHandleThrowsException(): void
{
$command = new ScrapeChapter(
mangaId: 'test-manga',
chapterNumber: '2',
sourceId: 'test-source'
chapterId: '1'
);
$exception = new \Exception('Scraping failed');

View File

@@ -23,9 +23,7 @@ class ScrapeChapterTest extends AbstractApiTestCase
{
// Given
$payload = [
'chapterNumber' => 'chapter-123',
'sourceId' => 'source-456',
'mangaId' => 'manga-789',
'chapterId' => 'chapter-123',
];
// When
@@ -43,15 +41,14 @@ class ScrapeChapterTest extends AbstractApiTestCase
/** @var ScrapeChapter $message */
$message = $messages[0];
$this->assertInstanceOf(ScrapeChapter::class, $message);
$this->assertEquals('chapter-123', $message->chapterId);
}
public function testInitiateChapterScrapingWithInvalidPayload(): void
{
// Given
$payload = [
'chapterNumber' => '',
'sourceId' => 'source-456',
'mangaId' => 'manga-789',
'chapterId' => '',
];
// When
@@ -65,7 +62,7 @@ class ScrapeChapterTest extends AbstractApiTestCase
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'chapterNumber',
'propertyPath' => 'chapterId',
'message' => 'This value should not be blank.',
],
],