feat: refonte de la gestion des événements de création de mangas en remplaçant le MessageBus par un EventDispatcher. Ajout d'un écouteur d'événements MangaCreated pour gérer la récupération des chapitres après la création d'un manga. Implémentation d'un EventDispatcher basé sur Symfony Messenger.

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-07-31 16:11:16 +02:00
parent f1eb97f156
commit bec1572fcb
5 changed files with 53 additions and 8 deletions

View File

@@ -10,7 +10,7 @@ use App\Domain\Manga\Domain\Event\MangaCreated;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException; use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId; use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls; use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls;
use Symfony\Component\Messenger\MessageBusInterface; use App\Domain\Shared\Domain\Contract\EventDispatcherInterface;
readonly class CreateMangaFromMangadexHandler readonly class CreateMangaFromMangadexHandler
{ {
@@ -18,7 +18,7 @@ readonly class CreateMangaFromMangadexHandler
private MangaProviderInterface $mangaProvider, private MangaProviderInterface $mangaProvider,
private MangaRepositoryInterface $mangaRepository, private MangaRepositoryInterface $mangaRepository,
private ImageProcessorInterface $imageProcessor, private ImageProcessorInterface $imageProcessor,
private MessageBusInterface $messageBus private EventDispatcherInterface $eventDispatcher
) {} ) {}
public function handle(CreateMangaFromMangadex $command): void public function handle(CreateMangaFromMangadex $command): void
@@ -44,6 +44,6 @@ readonly class CreateMangaFromMangadexHandler
$this->mangaRepository->save($manga); $this->mangaRepository->save($manga);
$this->messageBus->dispatch(new MangaCreated($manga->getId()->getValue(), $command->externalId)); $this->eventDispatcher->dispatch(new MangaCreated($manga->getId()->getValue(), $command->externalId));
} }
} }

View File

@@ -1,21 +1,21 @@
<?php <?php
namespace App\Domain\Manga\Infrastructure\EventListener; namespace App\Domain\Manga\Application\EventListener;
use App\Domain\Manga\Application\Command\FetchMangaChapters; use App\Domain\Manga\Application\Command\FetchMangaChapters;
use App\Domain\Manga\Application\CommandHandler\FetchMangaChaptersHandler;
use App\Domain\Manga\Domain\Event\MangaCreated; use App\Domain\Manga\Domain\Event\MangaCreated;
use App\Domain\Manga\Domain\Model\ValueObject\MangaId; use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
use Symfony\Component\Messenger\MessageBusInterface;
readonly class MangaCreatedListener readonly class MangaCreatedEventListener
{ {
public function __construct( public function __construct(
private MessageBusInterface $messageBus private FetchMangaChaptersHandler $fetchMangaChaptersHandler
) {} ) {}
public function __invoke(MangaCreated $event): void public function __invoke(MangaCreated $event): void
{ {
$this->messageBus->dispatch( $this->fetchMangaChaptersHandler->handle(
new FetchMangaChapters(new MangaId($event->mangaId)) new FetchMangaChapters(new MangaId($event->mangaId))
); );
} }

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Domain\Manga\Infrastructure\MessageHandler;
use App\Domain\Manga\Application\EventListener\MangaCreatedEventListener;
use App\Domain\Manga\Domain\Event\MangaCreated;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
readonly class MangaCreatedMessageHandler
{
public function __construct(private MangaCreatedEventListener $listener)
{
}
public function __invoke(MangaCreated $event): void
{
$this->listener->__invoke($event);
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Domain\Shared\Domain\Contract;
interface EventDispatcherInterface
{
public function dispatch(object $event): void;
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Domain\Shared\Infrastructure\Messenger;
use App\Domain\Shared\Domain\Contract\EventDispatcherInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class SymfonyMessengerEventDispatcher implements EventDispatcherInterface
{
public function __construct(private MessageBusInterface $bus)
{
}
public function dispatch(object $event): void
{
$this->bus->dispatch($event);
}
}