diff --git a/config/services.yaml b/config/services.yaml index 0c35be7..777ffb9 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -116,3 +116,7 @@ services: arguments: $publicDir: '%kernel.project_dir%/public' $httpClient: '@GuzzleHttp\Client' + + App\Domain\Manga\Infrastructure\EventListener\MangaCreatedListener: + tags: + - { name: messenger.message_handler } diff --git a/src/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandler.php b/src/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandler.php index 149bc42..95373ab 100644 --- a/src/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandler.php +++ b/src/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandler.php @@ -6,16 +6,19 @@ use App\Domain\Manga\Application\Command\CreateMangaFromMangadex; use App\Domain\Manga\Domain\Contract\Provider\MangaProviderInterface; use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface; use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface; +use App\Domain\Manga\Domain\Event\MangaCreated; use App\Domain\Manga\Domain\Exception\MangaNotFoundException; use App\Domain\Manga\Domain\Model\ValueObject\ExternalId; use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls; +use Symfony\Component\Messenger\MessageBusInterface; readonly class CreateMangaFromMangadexHandler { public function __construct( private MangaProviderInterface $mangaProvider, private MangaRepositoryInterface $mangaRepository, - private ImageProcessorInterface $imageProcessor + private ImageProcessorInterface $imageProcessor, + private MessageBusInterface $messageBus ) {} public function handle(CreateMangaFromMangadex $command): void @@ -40,5 +43,7 @@ readonly class CreateMangaFromMangadexHandler } $this->mangaRepository->save($manga); + + $this->messageBus->dispatch(new MangaCreated($command->externalId)); } } \ No newline at end of file diff --git a/src/Domain/Manga/Application/CommandHandler/CreateMangaHandler.php b/src/Domain/Manga/Application/CommandHandler/CreateMangaHandler.php index 50116e4..ddee5ef 100644 --- a/src/Domain/Manga/Application/CommandHandler/CreateMangaHandler.php +++ b/src/Domain/Manga/Application/CommandHandler/CreateMangaHandler.php @@ -5,6 +5,7 @@ namespace App\Domain\Manga\Application\CommandHandler; use App\Domain\Manga\Application\Command\CreateManga; use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface; use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface; +use App\Domain\Manga\Domain\Event\MangaCreated; use App\Domain\Manga\Domain\Model\Manga; use App\Domain\Manga\Domain\Model\ValueObject\ExternalId; use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls; @@ -12,12 +13,14 @@ use App\Domain\Manga\Domain\Model\ValueObject\MangaId; use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug; use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle; use Ramsey\Uuid\Uuid; +use Symfony\Component\Messenger\MessageBusInterface; readonly class CreateMangaHandler { public function __construct( private MangaRepositoryInterface $mangaRepository, - private ImageProcessorInterface $imageProcessor + private ImageProcessorInterface $imageProcessor, + private MessageBusInterface $messageBus ) {} public function handle(CreateManga $command): void @@ -47,5 +50,9 @@ readonly class CreateMangaHandler } $this->mangaRepository->save($manga); + + if ($command->externalId) { + $this->messageBus->dispatch(new MangaCreated($command->externalId)); + } } } \ No newline at end of file diff --git a/src/Domain/Manga/Domain/Event/MangaCreated.php b/src/Domain/Manga/Domain/Event/MangaCreated.php new file mode 100644 index 0000000..4c31c5f --- /dev/null +++ b/src/Domain/Manga/Domain/Event/MangaCreated.php @@ -0,0 +1,10 @@ +messageBus->dispatch( + new FetchMangaChapters($event->externalId) + ); + } +} \ No newline at end of file diff --git a/tests/Domain/Manga/Adapter/InMemoryMangadexClient.php b/tests/Domain/Manga/Adapter/InMemoryMangadexClient.php index a7f27bb..1996aea 100644 --- a/tests/Domain/Manga/Adapter/InMemoryMangadexClient.php +++ b/tests/Domain/Manga/Adapter/InMemoryMangadexClient.php @@ -9,7 +9,6 @@ class InMemoryMangadexClient implements MangadexClientInterface private array $mangas = []; private array $feeds = []; private array $aggregates = []; - private array $mangaFeeds = []; public function __construct( array $mangas = [], @@ -62,7 +61,22 @@ class InMemoryMangadexClient implements MangadexClientInterface public function getMangaFeed(string $mangaId, int $offset = 0, int $limit = 500, string $order = 'asc'): array { - return $this->mangaFeeds[$mangaId] ?? ['data' => []]; + if (!isset($this->feeds[$mangaId])) { + return [ + 'data' => [], + 'total' => 0 + ]; + } + + $feed = $this->feeds[$mangaId]; + if ($order === 'desc') { + $feed = array_reverse($feed); + } + + return [ + 'data' => array_slice($feed, $offset, $limit), + 'total' => count($feed) + ]; } public function getMangaAggregate(string $mangaId): array @@ -106,9 +120,4 @@ class InMemoryMangadexClient implements MangadexClientInterface { $this->aggregates[$mangaId] = $aggregate; } - - public function setMangaFeed(string $mangaId, array $feed): void - { - $this->mangaFeeds[$mangaId] = $feed; - } } \ No newline at end of file diff --git a/tests/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandlerTest.php b/tests/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandlerTest.php index ca5c5d2..85b21dc 100644 --- a/tests/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandlerTest.php +++ b/tests/Domain/Manga/Application/CommandHandler/CreateMangaFromMangadexHandlerTest.php @@ -13,6 +13,7 @@ use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle; use App\Tests\Domain\Manga\Adapter\InMemoryMangaProvider; use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository; use App\Tests\Domain\Manga\Adapter\InMemoryImageProcessor; +use App\Tests\Shared\Adapter\InMemoryMessageBus; use PHPUnit\Framework\TestCase; class CreateMangaFromMangadexHandlerTest extends TestCase @@ -21,7 +22,7 @@ class CreateMangaFromMangadexHandlerTest extends TestCase private InMemoryMangaRepository $repository; private InMemoryImageProcessor $imageProcessor; private CreateMangaFromMangadexHandler $handler; - + private InMemoryMessageBus $messageBus; protected function setUp(): void { $manga = new Manga( @@ -40,10 +41,12 @@ class CreateMangaFromMangadexHandlerTest extends TestCase $this->provider = new InMemoryMangaProvider([$manga]); $this->repository = new InMemoryMangaRepository(); $this->imageProcessor = new InMemoryImageProcessor(); + $this->messageBus = new InMemoryMessageBus(); $this->handler = new CreateMangaFromMangadexHandler( $this->provider, $this->repository, - $this->imageProcessor + $this->imageProcessor, + $this->messageBus ); } diff --git a/tests/Domain/Manga/Application/CommandHandler/CreateMangaHandlerTest.php b/tests/Domain/Manga/Application/CommandHandler/CreateMangaHandlerTest.php index 6d1e20d..b263f22 100644 --- a/tests/Domain/Manga/Application/CommandHandler/CreateMangaHandlerTest.php +++ b/tests/Domain/Manga/Application/CommandHandler/CreateMangaHandlerTest.php @@ -6,6 +6,7 @@ use App\Domain\Manga\Application\Command\CreateManga; use App\Domain\Manga\Application\CommandHandler\CreateMangaHandler; use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository; use App\Tests\Domain\Manga\Adapter\InMemoryImageProcessor; +use App\Tests\Shared\Adapter\InMemoryMessageBus; use PHPUnit\Framework\TestCase; class CreateMangaHandlerTest extends TestCase @@ -13,14 +14,16 @@ class CreateMangaHandlerTest extends TestCase private InMemoryMangaRepository $repository; private InMemoryImageProcessor $imageProcessor; private CreateMangaHandler $handler; - + private InMemoryMessageBus $messageBus; protected function setUp(): void { $this->repository = new InMemoryMangaRepository(); $this->imageProcessor = new InMemoryImageProcessor(); + $this->messageBus = new InMemoryMessageBus(); $this->handler = new CreateMangaHandler( $this->repository, - $this->imageProcessor + $this->imageProcessor, + $this->messageBus ); } diff --git a/tests/Domain/Manga/Application/CommandHandler/FetchMangaChaptersHandlerTest.php b/tests/Domain/Manga/Application/CommandHandler/FetchMangaChaptersHandlerTest.php index fa05a6f..f1c32cd 100644 --- a/tests/Domain/Manga/Application/CommandHandler/FetchMangaChaptersHandlerTest.php +++ b/tests/Domain/Manga/Application/CommandHandler/FetchMangaChaptersHandlerTest.php @@ -46,15 +46,13 @@ class FetchMangaChaptersHandlerTest extends TestCase $this->mangaRepository->save($manga); - $this->mangadexClient->setMangaFeed($externalId, [ - 'data' => [ - [ - 'id' => 'chapter-1', - 'attributes' => [ - 'chapter' => '1', - 'title' => 'Chapter 1', - 'volume' => '1' - ] + $this->mangadexClient->addFeed($externalId, [ + [ + 'id' => 'chapter-1', + 'attributes' => [ + 'chapter' => '1', + 'title' => 'Chapter 1', + 'volume' => '1' ] ] ]); @@ -75,4 +73,4 @@ class FetchMangaChaptersHandlerTest extends TestCase $command = new FetchMangaChapters($externalId); $this->handler->handle($command); } -} \ No newline at end of file +} diff --git a/tests/Feature/ApiTestCase.php b/tests/Feature/ApiTestCase.php deleted file mode 100644 index 0519ecb..0000000 --- a/tests/Feature/ApiTestCase.php +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file