feat: mise à jour de la gestion des sources de contenu pour permettre l'importation et la mise à jour des sources existantes, avec ajout de la méthode findByBaseUrl dans le repository.
This commit is contained in:
parent
dac2f91998
commit
a00858ae6e
@@ -20,28 +20,40 @@ readonly class ImportContentSourceCommandHandler
|
||||
throw new InvalidArgumentException('Content sources must be an array');
|
||||
}
|
||||
|
||||
$contentSourcesToImport = [];
|
||||
|
||||
foreach ($command->contentSources as $data) {
|
||||
if (!$this->isValidContentSourceData($data)) {
|
||||
throw new InvalidArgumentException('Invalid content source data provided');
|
||||
}
|
||||
|
||||
$contentSource = ContentSource::create(
|
||||
baseUrl: $data['baseUrl'],
|
||||
chapterUrlFormat: $data['chapterUrlFormat'],
|
||||
scrapingType: $data['scrapingType'],
|
||||
imageSelector: $data['imageSelector'] ?? null,
|
||||
nextPageSelector: $data['nextPageSelector'] ?? null,
|
||||
chapterSelector: $data['chapterSelector'] ?? null,
|
||||
);
|
||||
// Recherche d'une source existante avec la même baseUrl
|
||||
$existingContentSource = $this->contentSourceRepository->findByBaseUrl($data['baseUrl']);
|
||||
|
||||
$contentSourcesToImport[] = $contentSource;
|
||||
if ($existingContentSource) {
|
||||
// Met à jour la source existante
|
||||
$existingContentSource->update(
|
||||
baseUrl: $data['baseUrl'],
|
||||
chapterUrlFormat: $data['chapterUrlFormat'],
|
||||
scrapingType: $data['scrapingType'],
|
||||
imageSelector: $data['imageSelector'] ?? null,
|
||||
nextPageSelector: $data['nextPageSelector'] ?? null,
|
||||
chapterSelector: $data['chapterSelector'] ?? null,
|
||||
);
|
||||
|
||||
$this->contentSourceRepository->save($existingContentSource);
|
||||
} else {
|
||||
// Crée une nouvelle source
|
||||
$newContentSource = ContentSource::create(
|
||||
baseUrl: $data['baseUrl'],
|
||||
chapterUrlFormat: $data['chapterUrlFormat'],
|
||||
scrapingType: $data['scrapingType'],
|
||||
imageSelector: $data['imageSelector'] ?? null,
|
||||
nextPageSelector: $data['nextPageSelector'] ?? null,
|
||||
chapterSelector: $data['chapterSelector'] ?? null,
|
||||
);
|
||||
|
||||
$this->contentSourceRepository->save($newContentSource);
|
||||
}
|
||||
}
|
||||
|
||||
// Supprime tous les content sources existants puis importe les nouveaux
|
||||
$this->contentSourceRepository->deleteAll();
|
||||
$this->contentSourceRepository->saveMultiple($contentSourcesToImport);
|
||||
}
|
||||
|
||||
private function isValidContentSourceData(mixed $data): bool
|
||||
|
||||
@@ -8,6 +8,7 @@ interface ContentSourceRepositoryInterface
|
||||
{
|
||||
public function findAll(): array;
|
||||
public function findById(int $id): ?ContentSource;
|
||||
public function findByBaseUrl(string $baseUrl): ?ContentSource;
|
||||
public function save(ContentSource $contentSource): void;
|
||||
public function delete(ContentSource $contentSource): void;
|
||||
public function deleteAll(): void;
|
||||
|
||||
@@ -32,6 +32,14 @@ readonly class DoctrineContentSourceRepository implements ContentSourceRepositor
|
||||
return $entity ? $this->mapper->toDomain($entity) : null;
|
||||
}
|
||||
|
||||
public function findByBaseUrl(string $baseUrl): ?ContentSource
|
||||
{
|
||||
$entity = $this->entityManager->getRepository(ContentSourceEntity::class)
|
||||
->findOneBy(['baseUrl' => $baseUrl]);
|
||||
|
||||
return $entity ? $this->mapper->toDomain($entity) : null;
|
||||
}
|
||||
|
||||
public function save(ContentSource $contentSource): void
|
||||
{
|
||||
if ($contentSource->getId()) {
|
||||
|
||||
Reference in New Issue
Block a user