From 6559597db56ca7d6d8d68d90bddfc4fad5d89c2d Mon Sep 17 00:00:00 2001 From: Carlos Buenosvinos Date: Mon, 7 Mar 2022 15:12:28 +0100 Subject: [PATCH] [Chapter9] WIP --- .../Event/NewAuthorSignedEventHandler.php | 6 ++-- ...enCreateTimelineProjectionEventHandler.php | 29 +++++++++++++++ .../Projection/CreateTimelineProjection.php | 35 +++++++++++++++++++ .../CreateTimelineProjectionHandler.php | 34 ++++++++++++++++++ ...eateTimelineProjectionHandlerInterface.php | 10 ++++++ .../Author/Query/TimelineQueryHandler.php | 2 +- .../SymfonyAuthorFollowedEventHandler.php | 2 +- .../SymfonyNewAuthorSignedEventHandler.php | 7 ++-- ...enCreateTimelineProjectionEventHandler.php | 32 +++++++++++++++++ 9 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php create mode 100644 src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjection.php create mode 100644 src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandler.php create mode 100644 src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandlerInterface.php create mode 100644 src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php diff --git a/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php b/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php index bfb55a1..28d6910 100644 --- a/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php +++ b/src/Cheeper/Chapter7/Application/Author/Event/NewAuthorSignedEventHandler.php @@ -6,19 +6,21 @@ namespace Cheeper\Chapter7\Application\Author\Event; use Cheeper\Chapter7\Application\Author\Projection\CreateFollowersCounterProjection; use Cheeper\Chapter7\Application\Author\Projection\CreateFollowersCounterProjectionHandlerInterface; +use Cheeper\Chapter7\Application\Author\Projection\CreateTimelineProjection; +use Cheeper\Chapter7\Application\Author\Projection\CreateTimelineProjectionHandlerInterface; use Cheeper\Chapter7\DomainModel\Author\NewAuthorSigned; //snippet new-author-signed-event-handler final class NewAuthorSignedEventHandler { public function __construct( - private CreateFollowersCounterProjectionHandlerInterface $projector + private CreateFollowersCounterProjectionHandlerInterface $followersProjector ) { } public function handle(NewAuthorSigned $event): void { - $this->projector->__invoke( + $this->followersProjector->__invoke( CreateFollowersCounterProjection::ofAuthor( $event->authorId(), $event->authorUsername() diff --git a/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php b/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php new file mode 100644 index 0000000..48da8e3 --- /dev/null +++ b/src/Cheeper/Chapter7/Application/Author/Event/WhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php @@ -0,0 +1,29 @@ +projector->__invoke( + CreateTimelineProjection::ofAuthor( + $event->authorId(), + $event->authorUsername() + ) + ); + } +} \ No newline at end of file diff --git a/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjection.php b/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjection.php new file mode 100644 index 0000000..f1af3d4 --- /dev/null +++ b/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjection.php @@ -0,0 +1,35 @@ +stampAsNewMessage(); + } + + public function authorId(): string + { + return $this->authorId; + } + + public function authorUsername(): string + { + return $this->authorUsername; + } +} diff --git a/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandler.php b/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandler.php new file mode 100644 index 0000000..c00f75b --- /dev/null +++ b/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandler.php @@ -0,0 +1,34 @@ +authorId()); + $key = sprintf('author_timeline_projection:%s', $authorId); + + $result = [ + 'id' => $projection->authorId(), + 'username' => $projection->authorUsername(), + 'followers' => 0, + ]; + + $this->redis->set( + 'author_followers_counter_projection:'.$authorId->toString(), + json_encode($result) + ); + } +} +//end-snippet diff --git a/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandlerInterface.php b/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandlerInterface.php new file mode 100644 index 0000000..d56f5a6 --- /dev/null +++ b/src/Cheeper/Chapter7/Application/Author/Projection/CreateTimelineProjectionHandlerInterface.php @@ -0,0 +1,10 @@ +authorId(); - $key = sprintf('timelines_%s', $authorId); + $key = sprintf('author_timeline_projection:%s', $authorId); $this->checkAuthorExists($key, $authorId); diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php index ddd01f1..b7abcd0 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyAuthorFollowedEventHandler.php @@ -25,7 +25,7 @@ final class SymfonyAuthorFollowedEventHandler implements MessageSubscriberInterf { yield AuthorFollowed::class => [ 'method' => 'handle', - 'from_transport' => 'chapter7_events', + '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 index 1c1a3c7..4b082b8 100644 --- a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyNewAuthorSignedEventHandler.php @@ -12,12 +12,13 @@ use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; final class SymfonyNewAuthorSignedEventHandler implements MessageSubscriberInterface { public function __construct( - private NewAuthorSignedEventHandler $eventHandler + private NewAuthorSignedEventHandler $eventHandler, ) { } - public function handle(NewAuthorSigned $event): void - { + public function handle( + NewAuthorSigned $event + ): void { $this->eventHandler->handle($event); } diff --git a/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php new file mode 100644 index 0000000..e4ad210 --- /dev/null +++ b/src/Cheeper/Chapter7/Infrastructure/Application/Author/Event/SymfonyWhenNewAuthorSignedThenCreateTimelineProjectionEventHandler.php @@ -0,0 +1,32 @@ +eventHandler->handle($event); + } + + public static function getHandledMessages(): iterable + { + yield NewAuthorSigned::class => [ + 'method' => 'handle', + 'from_transport' => 'events_async', + ]; + } +}