feat: debut rerefonte DDD CQRS
This commit is contained in:
committed by
ThysTips
parent
d4142012ec
commit
0e3d72cc5e
11
src/Domain/Scraping/Application/Command/ScrapeChapter.php
Normal file
11
src/Domain/Scraping/Application/Command/ScrapeChapter.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Application\Command\ScrapeChapter;
|
||||
|
||||
class ScrapeChapterCommand
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $chapterId,
|
||||
public readonly string $sourceId
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Application\Command\ScrapeChapter;
|
||||
|
||||
use App\Domain\Scraping\Domain\Contract\ScraperInterface;
|
||||
use App\Domain\Scraping\Domain\Repository\ScrapingJobRepositoryInterface;
|
||||
use App\Domain\Scraping\Domain\Event\ChapterScrapingStarted;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class ScrapeChapterHandler
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ScraperInterface $scraper,
|
||||
private readonly ScrapingJobRepositoryInterface $scrapingJobRepository,
|
||||
private readonly MessageBusInterface $eventBus
|
||||
) {}
|
||||
|
||||
public function handle(ScrapeChapterCommand $command): void
|
||||
{
|
||||
$job = $this->scraper->createScrapingJob(
|
||||
$command->chapterId,
|
||||
$command->sourceId
|
||||
);
|
||||
|
||||
$this->scrapingJobRepository->save($job);
|
||||
|
||||
$this->eventBus->dispatch(new ChapterScrapingStarted($job->getId()));
|
||||
|
||||
$this->scraper->scrape($job);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Repository;
|
||||
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
|
||||
interface ScrapingJobRepositoryInterface
|
||||
{
|
||||
public function save(ScrapingJob $job): void;
|
||||
public function findById(string $id): ?ScrapingJob;
|
||||
public function findByChapterId(string $chapterId): ?ScrapingJob;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Contract;
|
||||
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
|
||||
interface ScraperInterface
|
||||
{
|
||||
public function createScrapingJob(string $chapterId, string $sourceId): ScrapingJob;
|
||||
public function scrape(ScrapingJob $job): void;
|
||||
public function supports(string $sourceType): bool;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Event;
|
||||
|
||||
class ChapterScrapingCompleted
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $jobId,
|
||||
private readonly array $scrapedPages
|
||||
) {}
|
||||
|
||||
public function getJobId(): string
|
||||
{
|
||||
return $this->jobId;
|
||||
}
|
||||
|
||||
public function getScrapedPages(): array
|
||||
{
|
||||
return $this->scrapedPages;
|
||||
}
|
||||
}
|
||||
15
src/Domain/Scraping/Domain/Event/ChapterScrapingStarted.php
Normal file
15
src/Domain/Scraping/Domain/Event/ChapterScrapingStarted.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Event;
|
||||
|
||||
class ChapterScrapingStarted
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $jobId
|
||||
) {}
|
||||
|
||||
public function getJobId(): string
|
||||
{
|
||||
return $this->jobId;
|
||||
}
|
||||
}
|
||||
23
src/Domain/Scraping/Domain/Event/PageScrapingProgressed.php
Normal file
23
src/Domain/Scraping/Domain/Event/PageScrapingProgressed.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Event;
|
||||
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingProgress;
|
||||
|
||||
class PageScrapingProgressed
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $jobId,
|
||||
private readonly ScrapingProgress $progress
|
||||
) {}
|
||||
|
||||
public function getJobId(): string
|
||||
{
|
||||
return $this->jobId;
|
||||
}
|
||||
|
||||
public function getProgress(): ScrapingProgress
|
||||
{
|
||||
return $this->progress;
|
||||
}
|
||||
}
|
||||
84
src/Domain/Scraping/Domain/Model/ScrapingJob.php
Normal file
84
src/Domain/Scraping/Domain/Model/ScrapingJob.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Model;
|
||||
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\ImageUrl;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\PageNumber;
|
||||
|
||||
class ScrapingJob
|
||||
{
|
||||
private array $pages = [];
|
||||
private ScrapingStatus $status;
|
||||
private \DateTimeImmutable $createdAt;
|
||||
private ?\DateTimeImmutable $completedAt = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $id,
|
||||
private readonly string $chapterId,
|
||||
private readonly string $mangaId,
|
||||
private readonly string $sourceId
|
||||
) {
|
||||
$this->status = ScrapingStatus::PENDING;
|
||||
$this->createdAt = new \DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function addPage(PageNumber $pageNumber, ImageUrl $imageUrl): void
|
||||
{
|
||||
$this->pages[$pageNumber->getValue()] = $imageUrl->getValue();
|
||||
if ($this->status === ScrapingStatus::PENDING) {
|
||||
$this->status = ScrapingStatus::IN_PROGRESS;
|
||||
}
|
||||
}
|
||||
|
||||
public function complete(): void
|
||||
{
|
||||
$this->status = ScrapingStatus::COMPLETED;
|
||||
$this->completedAt = new \DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function fail(): void
|
||||
{
|
||||
$this->status = ScrapingStatus::FAILED;
|
||||
$this->completedAt = new \DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getChapterId(): string
|
||||
{
|
||||
return $this->chapterId;
|
||||
}
|
||||
|
||||
public function getMangaId(): string
|
||||
{
|
||||
return $this->mangaId;
|
||||
}
|
||||
|
||||
public function getSourceId(): string
|
||||
{
|
||||
return $this->sourceId;
|
||||
}
|
||||
|
||||
public function getPages(): array
|
||||
{
|
||||
return $this->pages;
|
||||
}
|
||||
|
||||
public function getStatus(): ScrapingStatus
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): \DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function getCompletedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->completedAt;
|
||||
}
|
||||
}
|
||||
19
src/Domain/Scraping/Domain/Model/ScrapingProgress.php
Normal file
19
src/Domain/Scraping/Domain/Model/ScrapingProgress.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Model;
|
||||
|
||||
class ScrapingProgress
|
||||
{
|
||||
public function __construct(
|
||||
private readonly int $pagesScraped,
|
||||
private readonly int $totalPages
|
||||
) {}
|
||||
|
||||
public function getPercentage(): float
|
||||
{
|
||||
if ($this->totalPages === 0) {
|
||||
return 0;
|
||||
}
|
||||
return ($this->pagesScraped / $this->totalPages) * 100;
|
||||
}
|
||||
}
|
||||
11
src/Domain/Scraping/Domain/Model/ScrapingStatus.php
Normal file
11
src/Domain/Scraping/Domain/Model/ScrapingStatus.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Model;
|
||||
|
||||
enum ScrapingStatus: string
|
||||
{
|
||||
case PENDING = 'pending';
|
||||
case IN_PROGRESS = 'in_progress';
|
||||
case COMPLETED = 'completed';
|
||||
case FAILED = 'failed';
|
||||
}
|
||||
24
src/Domain/Scraping/Domain/Model/ValueObject/ImageUrl.php
Normal file
24
src/Domain/Scraping/Domain/Model/ValueObject/ImageUrl.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Model\ValueObject;
|
||||
|
||||
class ImageUrl
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $url
|
||||
) {
|
||||
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
||||
throw new \InvalidArgumentException('Invalid image URL provided');
|
||||
}
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function getExtension(): string
|
||||
{
|
||||
return pathinfo(parse_url($this->url, PHP_URL_PATH), PATHINFO_EXTENSION);
|
||||
}
|
||||
}
|
||||
24
src/Domain/Scraping/Domain/Model/ValueObject/PageNumber.php
Normal file
24
src/Domain/Scraping/Domain/Model/ValueObject/PageNumber.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Domain\Model\ValueObject;
|
||||
|
||||
class PageNumber
|
||||
{
|
||||
public function __construct(
|
||||
private readonly int $number
|
||||
) {
|
||||
if ($number < 1) {
|
||||
throw new \InvalidArgumentException('Page number must be greater than 0');
|
||||
}
|
||||
}
|
||||
|
||||
public function getValue(): int
|
||||
{
|
||||
return $this->number;
|
||||
}
|
||||
|
||||
public function getFormattedNumber(): string
|
||||
{
|
||||
return sprintf('%03d', $this->number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Infrastructure\Persistence;
|
||||
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use App\Domain\Scraping\Domain\Repository\ScrapingJobRepositoryInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class DoctrineScrapingJobRepository implements ScrapingJobRepositoryInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager
|
||||
) {}
|
||||
|
||||
public function save(ScrapingJob $job): void
|
||||
{
|
||||
$this->entityManager->persist($job);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
public function findById(string $id): ?ScrapingJob
|
||||
{
|
||||
return $this->entityManager->getRepository(ScrapingJob::class)->find($id);
|
||||
}
|
||||
|
||||
public function findByChapterId(string $chapterId): ?ScrapingJob
|
||||
{
|
||||
return $this->entityManager->getRepository(ScrapingJob::class)
|
||||
->findOneBy(['chapterId' => $chapterId]);
|
||||
}
|
||||
|
||||
public function findPendingJobs(): array
|
||||
{
|
||||
return $this->entityManager->getRepository(ScrapingJob::class)
|
||||
->createQueryBuilder('sj')
|
||||
->where('sj.status = :status')
|
||||
->setParameter('status', 'pending')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function findInProgressJobs(): array
|
||||
{
|
||||
return $this->entityManager->getRepository(ScrapingJob::class)
|
||||
->createQueryBuilder('sj')
|
||||
->where('sj.status = :status')
|
||||
->setParameter('status', 'in_progress')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Infrastructure\Persistence\Entity;
|
||||
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingStatus;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'scraping_jobs')]
|
||||
class ScrapingJobEntity
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: 'string', length: 36)]
|
||||
private string $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $chapterId;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $mangaId;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $sourceId;
|
||||
|
||||
#[ORM\Column(type: 'json')]
|
||||
private array $pages = [];
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $status;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $createdAt;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
private ?\DateTimeImmutable $completedAt = null;
|
||||
|
||||
public static function fromDomain(ScrapingJob $job): self
|
||||
{
|
||||
$entity = new self();
|
||||
$entity->id = $job->getId();
|
||||
$entity->chapterId = $job->getChapterId();
|
||||
$entity->mangaId = $job->getMangaId();
|
||||
$entity->sourceId = $job->getSourceId();
|
||||
$entity->pages = $job->getPages();
|
||||
$entity->status = $job->getStatus()->value;
|
||||
$entity->createdAt = $job->getCreatedAt();
|
||||
$entity->completedAt = $job->getCompletedAt();
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function toDomain(): ScrapingJob
|
||||
{
|
||||
$job = new ScrapingJob(
|
||||
$this->id,
|
||||
$this->chapterId,
|
||||
$this->mangaId,
|
||||
$this->sourceId
|
||||
);
|
||||
|
||||
// Reconstruire l'état du job à partir des données persistées
|
||||
$reflection = new \ReflectionClass(ScrapingJob::class);
|
||||
|
||||
$pagesProperty = $reflection->getProperty('pages');
|
||||
$pagesProperty->setAccessible(true);
|
||||
$pagesProperty->setValue($job, $this->pages);
|
||||
|
||||
$statusProperty = $reflection->getProperty('status');
|
||||
$statusProperty->setAccessible(true);
|
||||
$statusProperty->setValue($job, ScrapingStatus::from($this->status));
|
||||
|
||||
$createdAtProperty = $reflection->getProperty('createdAt');
|
||||
$createdAtProperty->setAccessible(true);
|
||||
$createdAtProperty->setValue($job, $this->createdAt);
|
||||
|
||||
$completedAtProperty = $reflection->getProperty('completedAt');
|
||||
$completedAtProperty->setAccessible(true);
|
||||
$completedAtProperty->setValue($job, $this->completedAt);
|
||||
|
||||
return $job;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Scraping\Infrastructure\Service\Scraper;
|
||||
|
||||
use App\Domain\Scraping\Domain\Contract\ScraperInterface as ContractScraperInterface;
|
||||
use App\Domain\Scraping\Domain\Service\ScraperInterface;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingJob;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\ImageUrl;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\PageNumber;
|
||||
use App\Domain\Scraping\Domain\Event\PageScrapingProgressed;
|
||||
use App\Domain\Scraping\Domain\Event\ChapterScrapingCompleted;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class HtmlScraper implements ContractScraperInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly EventDispatcherInterface $eventDispatcher
|
||||
) {}
|
||||
|
||||
public function createScrapingJob(string $chapterId, string $sourceId): ScrapingJob
|
||||
{
|
||||
return new ScrapingJob(
|
||||
uniqid('scraping_'),
|
||||
$chapterId,
|
||||
$sourceId
|
||||
);
|
||||
}
|
||||
|
||||
public function scrape(ScrapingJob $job): void
|
||||
{
|
||||
$url = $this->buildUrl($job); // À implémenter selon votre logique
|
||||
$response = $this->httpClient->request('GET', $url);
|
||||
|
||||
$crawler = new Crawler($response->getContent());
|
||||
$images = $crawler->filter('img.manga-page'); // Adapter selon le site cible
|
||||
|
||||
$pageNumber = 1;
|
||||
$images->each(function (Crawler $image) use ($job, $pageNumber) {
|
||||
$imageUrl = new ImageUrl($image->attr('src'));
|
||||
$job->addPage(new PageNumber($pageNumber), $imageUrl);
|
||||
|
||||
$this->eventDispatcher->dispatch(
|
||||
new PageScrapingProgressed($job->getId(), $job->getProgress())
|
||||
);
|
||||
|
||||
$pageNumber++;
|
||||
});
|
||||
|
||||
$this->eventDispatcher->dispatch(
|
||||
new ChapterScrapingCompleted($job->getId(), $job->getPages())
|
||||
);
|
||||
}
|
||||
|
||||
public function supports(string $sourceType): bool
|
||||
{
|
||||
return $sourceType === 'html';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user