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:
parent
e29433bb0c
commit
c9f1771522
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user