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

@@ -29,6 +29,11 @@ doctrine:
dir: '%kernel.project_dir%/src/Domain/Shared/Infrastructure/Persistence/Entity' dir: '%kernel.project_dir%/src/Domain/Shared/Infrastructure/Persistence/Entity'
prefix: 'App\Domain\Shared\Infrastructure\Persistence\Entity' prefix: 'App\Domain\Shared\Infrastructure\Persistence\Entity'
alias: Shared alias: Shared
Scraping:
is_bundle: false
dir: '%kernel.project_dir%/src/Domain/Scraping/Infrastructure/Persistence/Entity'
prefix: 'App\Domain\Scraping\Infrastructure\Persistence\Entity'
alias: Scraping
when@test: when@test:
doctrine: doctrine:

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250626123300 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('DROP SEQUENCE manga_preferred_source_order_id_seq CASCADE');
$this->addSql('CREATE SEQUENCE manga_preferred_sources_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE manga_preferred_sources (id INT NOT NULL, manga_id INT NOT NULL, ordered_source_ids JSON NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_manga_preferred_sources_manga_id ON manga_preferred_sources (manga_id)');
$this->addSql('COMMENT ON COLUMN manga_preferred_sources.created_at IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN manga_preferred_sources.updated_at IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('DROP TABLE manga_preferred_source_order');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('DROP SEQUENCE manga_preferred_sources_id_seq CASCADE');
$this->addSql('CREATE SEQUENCE manga_preferred_source_order_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE manga_preferred_source_order (id INT NOT NULL, manga_id INT NOT NULL, source_id INT NOT NULL, "position" INT NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX idx_source_id ON manga_preferred_source_order (source_id)');
$this->addSql('CREATE INDEX idx_manga_id ON manga_preferred_source_order (manga_id)');
$this->addSql('CREATE UNIQUE INDEX unique_manga_source ON manga_preferred_source_order (manga_id, source_id)');
$this->addSql('COMMENT ON COLUMN manga_preferred_source_order.created_at IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('DROP INDEX UNIQ_manga_preferred_sources_manga_id');
$this->addSql('DROP TABLE manga_preferred_sources');
}
}

View File

@@ -56,7 +56,6 @@ readonly class ScrapeChapterHandler
// 3. Détermination des sources à utiliser // 3. Détermination des sources à utiliser
$sources = $this->getSourcesToTry($manga); $sources = $this->getSourcesToTry($manga);
dd($sources);
if (empty($sources)) { if (empty($sources)) {
throw new \InvalidArgumentException("No sources available for scraping"); throw new \InvalidArgumentException("No sources available for scraping");
} }

View File

@@ -30,17 +30,20 @@ readonly class GetMangaPreferredSourcesHandler
if ($manga->hasPreferredSources()) { if ($manga->hasPreferredSources()) {
$preferredSourceIds = $manga->getPreferredSources(); $preferredSourceIds = $manga->getPreferredSources();
// Créer un index des sources par ID pour optimiser les performances
$sourcesByIds = [];
foreach ($allActiveSources as $source) {
$sourcesByIds[$source->getId()->getValue()] = $source;
}
// Séparer les sources préférées et les autres // Séparer les sources préférées et les autres
$preferredSources = []; $preferredSources = [];
$otherSources = []; $otherSources = [];
// D'abord, ajouter les sources préférées dans l'ordre de préférence // D'abord, ajouter les sources préférées dans l'ordre de préférence exactement
foreach ($preferredSourceIds as $preferredId) { foreach ($preferredSourceIds as $preferredId) {
foreach ($allActiveSources as $source) { if (isset($sourcesByIds[$preferredId])) {
if ($source->getId()->getValue() === $preferredId) { $preferredSources[] = $sourcesByIds[$preferredId];
$preferredSources[] = $source;
break;
}
} }
} }

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