fix: preferred chapter fix

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-06-26 14:51:00 +02:00
parent d753761556
commit 4dc6e5cfab
6 changed files with 162 additions and 25 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Domain\Scraping\Infrastructure\Persistence;
use App\Domain\Scraping\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Scraping\Domain\Model\Manga;
use App\Domain\Scraping\Infrastructure\Persistence\Entity\MangaPreferredSourceEntity;
use App\Entity\Manga as EntityManga;
use App\Entity\ContentSource;
use Doctrine\ORM\EntityManagerInterface;
@@ -24,10 +25,13 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
return null;
}
// Récupération des sources préférées
// Récupération des sources préférées avec l'ordre depuis la nouvelle entité
$preferredSourceIds = [];
foreach ($mangaEntity->getPreferredSources() as $source) {
$preferredSourceIds[] = (string) $source->getId();
$mangaPreferredSource = $this->entityManager->getRepository(MangaPreferredSourceEntity::class)
->findOneBy(['mangaId' => $mangaEntity->getId()]);
if ($mangaPreferredSource) {
$preferredSourceIds = $mangaPreferredSource->getOrderedSourceIds();
}
return new Manga(
@@ -50,26 +54,30 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
throw new \InvalidArgumentException("Manga not found with ID: {$mangaId}");
}
// Si pas de sources, vider les sources préférées
if (empty($sourceIds)) {
$mangaEntity->setPreferredSources([]);
$this->entityManager->flush();
return;
// Récupérer ou créer l'entité MangaPreferredSource
$mangaPreferredSource = $this->entityManager->getRepository(MangaPreferredSourceEntity::class)
->findOneBy(['mangaId' => (int) $mangaId]);
if (!$mangaPreferredSource) {
$mangaPreferredSource = new MangaPreferredSourceEntity();
$mangaPreferredSource->setMangaId((int) $mangaId);
}
// Récupérer les sources existantes
$sources = $this->entityManager->getRepository(ContentSource::class)->findBy(['id' => $sourceIds]);
// Si pas de sources, vider les sources préférées
if (empty($sourceIds)) {
$mangaPreferredSource->setOrderedSourceIds([]);
} else {
// Valider que toutes les sources existent avant de les sauvegarder
$sources = $this->entityManager->getRepository(ContentSource::class)->findBy(['id' => $sourceIds]);
$existingSourceIds = array_map(fn($source) => (string) $source->getId(), $sources);
// Maintenir l'ordre exact des sources comme dans l'ancien controller
$orderedPreferredSources = array_map(
fn ($id) => current(array_filter($sources, fn ($s) => $s->getId() == $id)),
$sourceIds
);
// Garder uniquement les sources qui existent et maintenir l'ordre
$validSourceIds = array_values(array_intersect($sourceIds, $existingSourceIds));
// Filtrer les sources nulles (au cas où certaines n'existeraient pas)
$validSources = array_filter($orderedPreferredSources);
$mangaPreferredSource->setOrderedSourceIds($validSourceIds);
}
$mangaEntity->setPreferredSources($validSources);
$this->entityManager->persist($mangaPreferredSource);
$this->entityManager->flush();
}
}