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

@@ -0,0 +1,76 @@
<?php
namespace App\Domain\Scraping\Infrastructure\Persistence\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'manga_preferred_sources')]
#[ORM\UniqueConstraint(name: 'UNIQ_manga_preferred_sources_manga_id', columns: ['manga_id'])]
class MangaPreferredSourceEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: 'integer')]
private int $mangaId;
#[ORM\Column(type: Types::JSON)]
private array $orderedSourceIds = [];
#[ORM\Column]
private \DateTimeImmutable $createdAt;
#[ORM\Column]
private \DateTimeImmutable $updatedAt;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getMangaId(): int
{
return $this->mangaId;
}
public function setMangaId(int $mangaId): self
{
$this->mangaId = $mangaId;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getOrderedSourceIds(): array
{
return $this->orderedSourceIds;
}
public function setOrderedSourceIds(array $orderedSourceIds): self
{
$this->orderedSourceIds = $orderedSourceIds;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
public function getUpdatedAt(): \DateTimeImmutable
{
return $this->updatedAt;
}
}

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();
}
}