feat: debut rerefonte DDD CQRS
This commit is contained in:
committed by
ThysTips
parent
8f7b5d71c5
commit
0c8ca6cca9
@@ -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