feat: ajout de la gestion des sources de contenu avec des commandes et des gestionnaires pour l'importation, la mise à jour et l'exportation, ainsi que la création des ressources API correspondantes.
This commit is contained in:
parent
ebcca466a9
commit
32b4e4fbb2
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Setting\Application\CommandHandler;
|
||||
|
||||
use App\Domain\Setting\Application\Command\ImportContentSourceCommand;
|
||||
use App\Domain\Setting\Domain\Contract\Repository\ContentSourceRepositoryInterface;
|
||||
use App\Domain\Setting\Domain\Model\ContentSource;
|
||||
use InvalidArgumentException;
|
||||
|
||||
readonly class ImportContentSourceCommandHandler
|
||||
{
|
||||
public function __construct(
|
||||
private ContentSourceRepositoryInterface $contentSourceRepository
|
||||
) {}
|
||||
|
||||
public function handle(ImportContentSourceCommand $command): void
|
||||
{
|
||||
// Validation des données d'import
|
||||
if (!is_array($command->contentSources)) {
|
||||
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,
|
||||
);
|
||||
|
||||
$contentSourcesToImport[] = $contentSource;
|
||||
}
|
||||
|
||||
// Supprime tous les content sources existants puis importe les nouveaux
|
||||
$this->contentSourceRepository->deleteAll();
|
||||
$this->contentSourceRepository->saveMultiple($contentSourcesToImport);
|
||||
}
|
||||
|
||||
private function isValidContentSourceData(mixed $data): bool
|
||||
{
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$requiredFields = ['baseUrl', 'chapterUrlFormat', 'scrapingType'];
|
||||
foreach ($requiredFields as $field) {
|
||||
if (!isset($data[$field]) || !is_string($data[$field]) || empty($data[$field])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user