From a856403b49be3712aee7c00b97dad85e8552d6c0 Mon Sep 17 00:00:00 2001 From: Carlos Buenosvinos Date: Sat, 12 Mar 2022 12:16:32 +0100 Subject: [PATCH] [Chapter 7] Multiple improvements + Event Handler + Projection + Projection Bus --- config/packages/messenger.yaml | 3 +-- config/services.yaml | 13 ++++++++----- docker-compose.yaml | 5 ++--- .../Author/Event/AuthorFollowedEventHandler.php | 2 +- .../Author/Event/NewAuthorSignedEventHandler.php | 2 +- ...gnedThenCreateTimelineProjectionEventHandler.php | 2 +- .../Cheep/Event/CheepPostedEventHandler.php | 2 +- ...hp => SymfonyAuthorFollowedEventHandler.php.old} | 1 + ...p => SymfonyNewAuthorSignedEventHandler.php.old} | 1 + ...henCreateTimelineProjectionEventHandler.php.old} | 1 + ...r.php => SymfonyCheepPostedEventHandler.php.old} | 3 ++- ...fonyAddCheepToTimelineProjectionHandler.php.old} | 3 ++- 12 files changed, 22 insertions(+), 16 deletions(-) rename src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/{SymfonyAuthorFollowedEventHandler.php => SymfonyAuthorFollowedEventHandler.php.old} (96%) rename src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/{SymfonyNewAuthorSignedEventHandler.php => SymfonyNewAuthorSignedEventHandler.php.old} (96%) rename src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/{SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php => SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php.old} (96%) rename src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/{SymfonyCheepPostedEventHandler.php => SymfonyCheepPostedEventHandler.php.old} (91%) rename src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/{SymfonyAddCheepToTimelineProjectionHandler.php => SymfonyAddCheepToTimelineProjectionHandler.php.old} (90%) diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index 95d81c9..1efbc6d 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -47,7 +47,6 @@ framework: # Dead letter box failed_messages: '%env(MESSENGER_TRANSPORT_BASE_DSN)%/failed_messages' - routing: # With a proper UX, almost all the commands should # be run asynchronously.However, some commands can @@ -69,4 +68,4 @@ framework: # Projections should run asynchronously by default. # However, it's possible to run some synchronously # without getting the performance benefits. - Cheeper\Chapter7\Application\Projection: projections_async \ No newline at end of file + Cheeper\Chapter7\Application\Projection: projections_async diff --git a/config/services.yaml b/config/services.yaml index 76f64a6..6aa693a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -104,8 +104,11 @@ services: # class: 'Cheeper\Chapter6\Infrastructure\Application\SymfonyProjectionBus' # CHAPTER 7 - Cheeper\Chapter7\: - resource: '../src/Cheeper/Chapter7/*' + Cheeper\Chapter7\Application\: + resource: '../src/Cheeper/Chapter7/Application/*' + + Cheeper\Chapter7\Infrastructure\: + resource: '../src/Cheeper/Chapter7/Infrastructure/*' Cheeper\Chapter7\DomainModel\Author\AuthorRepository: class: 'Cheeper\Chapter7\Infrastructure\DomainModel\Author\DoctrineOrmAuthorRepository' @@ -130,13 +133,13 @@ services: chapter7_command_handlers: namespace: Cheeper\Chapter7\Application\ - resource: '../src/Cheeper/Chapter7/Application/**/*CommandHandler.php' + resource: '../src/Cheeper/Chapter7/Application/**/Command/*CommandHandler.php' tags: - { name: messenger.message_handler, bus: command.bus } chapter7_event_handlers: namespace: Cheeper\Chapter7\Application\ - resource: '../src/Cheeper/Chapter7/Application/**/Symfony*EventHandler.php' + resource: '../src/Cheeper/Chapter7/Application/**/*EventHandler.php' tags: - { name: messenger.message_handler, bus: event.bus } @@ -148,7 +151,7 @@ services: chapter7_projection_handlers: namespace: Cheeper\Chapter7\Application\ - resource: '../src/Cheeper/Chapter7/Application/**/Symfony*ProjectionHandler.php' + resource: '../src/Cheeper/Chapter7/Application/**/*ProjectionHandler.php' tags: - { name: messenger.message_handler, bus: projection.bus } diff --git a/docker-compose.yaml b/docker-compose.yaml index 248db58..7530322 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,17 +1,16 @@ version: '3.9' - services: redis: image: redis ports: - "6379:6379" rabbitmq: - image: rabbitmq:management + ports: - "5672:5672" - "15672:15672" mysql: - image: mysql + image: mariadb ports: - "3306:3306" environment: diff --git a/src/Cheeper/Chapter7/Application/Author/Event/AuthorFollowedEventHandler.php b/src/Cheeper/Chapter7/Application/Author/Event/AuthorFollowedEventHandler.php index 7782670..bbdcfc5 100644 --- a/src/Cheeper/Chapter7/Application/Author/Event/AuthorFollowedEventHandler.php +++ b/src/Cheeper/Chapter7/Application/Author/Event/AuthorFollowedEventHandler.php @@ -16,7 +16,7 @@ final class AuthorFollowedEventHandler ) { } - public function handle(AuthorFollowed $event): void + public function __invoke(AuthorFollowed $event): void { $this->projectionHandler->__invoke( CountFollowersProjection::ofAuthor($event->toAuthorId()) diff --git a/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php b/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php index 28d6910..90ade16 100644 --- a/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php +++ b/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php @@ -18,7 +18,7 @@ final class NewAuthorSignedEventHandler ) { } - public function handle(NewAuthorSigned $event): void + public function __invoke(NewAuthorSigned $event): void { $this->followersProjector->__invoke( CreateFollowersCounterProjection::ofAuthor( diff --git a/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php b/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php index 48da8e3..bf31351 100644 --- a/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php +++ b/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php @@ -17,7 +17,7 @@ final class WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler ) { } - public function handle(NewAuthorSigned $event): void + public function __invoke(NewAuthorSigned $event): void { $this->projector->__invoke( CreateTimelineProjection::ofAuthor( diff --git a/src/Cheeper/Chapter7/Application/Cheep/Event/CheepPostedEventHandler.php b/src/Cheeper/Chapter7/Application/Cheep/Event/CheepPostedEventHandler.php index 14af5c3..f8bbc0b 100644 --- a/src/Cheeper/Chapter7/Application/Cheep/Event/CheepPostedEventHandler.php +++ b/src/Cheeper/Chapter7/Application/Cheep/Event/CheepPostedEventHandler.php @@ -21,7 +21,7 @@ final class CheepPostedEventHandler ) { } - public function handle(CheepPosted $event): void + public function __invoke(CheepPosted $event): void { $follows = $this->followRepository->toAuthorId( AuthorId::fromString($event->authorId()) diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php.old similarity index 96% rename from src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php rename to src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php.old index b7abcd0..eb6cbd5 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php.old @@ -25,6 +25,7 @@ final class SymfonyAuthorFollowedEventHandler implements MessageSubscriberInterf { yield AuthorFollowed::class => [ 'method' => 'handle', + 'bus' => 'event.bus', 'from_transport' => 'events_async', ]; } diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php.old similarity index 96% rename from src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php rename to src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php.old index 4b082b8..0a371bd 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php.old @@ -26,6 +26,7 @@ final class SymfonyNewAuthorSignedEventHandler implements MessageSubscriberInter { yield NewAuthorSigned::class => [ 'method' => 'handle', + 'bus' => 'event.bus', 'from_transport' => 'events_async', ]; } diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php.old similarity index 96% rename from src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php rename to src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php.old index e4ad210..9353bfc 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php.old @@ -26,6 +26,7 @@ final { yield NewAuthorSigned::class => [ 'method' => 'handle', + 'bus' => 'event.bus', 'from_transport' => 'events_async', ]; } diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/SymfonyCheepPostedEventHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/SymfonyCheepPostedEventHandler.php.old similarity index 91% rename from src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/SymfonyCheepPostedEventHandler.php rename to src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/SymfonyCheepPostedEventHandler.php.old index 5184c98..88576b6 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/SymfonyCheepPostedEventHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Event/SymfonyCheepPostedEventHandler.php.old @@ -25,7 +25,8 @@ final class SymfonyCheepPostedEventHandler implements MessageSubscriberInterface { yield CheepPosted::class => [ 'method' => 'handle', - 'from_transport' => 'chapter7_events', + 'bus' => 'event.bus', + 'from_transport' => 'events', ]; } } diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/SymfonyAddCheepToTimelineProjectionHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/SymfonyAddCheepToTimelineProjectionHandler.php.old similarity index 90% rename from src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/SymfonyAddCheepToTimelineProjectionHandler.php rename to src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/SymfonyAddCheepToTimelineProjectionHandler.php.old index 75c83f5..9229e46 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/SymfonyAddCheepToTimelineProjectionHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Cheep/Projection/SymfonyAddCheepToTimelineProjectionHandler.php.old @@ -25,7 +25,8 @@ final class SymfonyAddCheepToTimelineProjectionHandler implements MessageSubscri { yield AddCheepToTimelineProjection::class => [ 'method' => 'handle', - 'from_transport' => 'chapter7_async_projections', + 'bus' => 'projection.bus', + 'from_transport' => 'projections_async', ]; } }