From 8122e51c1f11859c9022ce553740986c505fdb3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Sat, 23 Apr 2016 02:09:35 +0200 Subject: [PATCH] Ditching type hinting for handlers and updating README --- README.md | 52 ++++----------------- src/CommandBus/CommandBus.php | 1 + src/CommandBus/Contracts/CommandHandler.php | 2 +- src/EventBus/Contracts/EventHandler.php | 5 -- src/EventBus/EventBus.php | 1 + src/QueryBus/Contracts/QueryHandler.php | 6 --- tests/CommandBus/DummyCommand.php | 3 ++ tests/CommandBus/DummyCommandHandler.php | 5 +- tests/EventBus/DummyEvent.php | 3 ++ tests/EventBus/DummyEventHandler.php | 2 +- tests/EventBus/SqliteEventHandler.php | 7 --- tests/QueryBus/DummyQuery.php | 3 ++ tests/QueryBus/DummyQueryHandler.php | 2 +- 13 files changed, 26 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 2562c15..a71308b 100644 --- a/README.md +++ b/README.md @@ -140,10 +140,8 @@ final class RegisterUserHandler implements CommandHandler $this->userRepository = $userRepository; } - public function __invoke(Command $command) - { - $this->guard($command); - + public function __invoke(RegisterUser $command) + { $user = new User( $command->getUsername(), $command->getPassword(), @@ -151,14 +149,7 @@ final class RegisterUserHandler implements CommandHandler ); $this->userRepository->save($user); - } - - private function guard(Command $command) - { - if (false === ($command instanceof RegisterUser)) { - throw new \InvalidArgumentException("Expected command of type: ".RegisterUser::class); - } - } + } } ``` @@ -328,21 +319,13 @@ final class GetUserHandler implements QueryHandler $this->userRepository = $userRepository; } - public function __invoke(Query $query) : QueryResponse - { - $this->guard($query); + public function __invoke(GetUser $query) : QueryResponse + { $userId = $query->getUserId(); $user = $this->userRepository->find($userId); return new UserQueryResponse($user); } - - private function guard(Query $query) - { - if (false === ($query instanceof GetUser)) { - throw new \InvalidArgumentException("Expected query of type: ".GetUser::class); - } - } } ``` @@ -543,19 +526,12 @@ final class SendWelcomeEmailHandler implements EventHandler $this->emailProvider = $emailProvider; } - public function __invoke(Event $event) + public function __invoke(UserRegistered $event) { $this->guard($event); $this->emailProvider->send('welcome_email', $event->getEmail()); } - - private function guard(Event $event) - { - if (false === ($event instanceof UserRegistered)) { - throw new \InvalidArgumentException("Expected event of type: ".UserRegistered::class); - } - } - + public static function subscribedTo() : string { return UserRegistered::class; @@ -583,19 +559,11 @@ final class SetupUserAccountHandler implements EventHandler $this->userCreditsRepository = $userCreditsRepository; } - public function __invoke(Event $event) - { - $this->guard($event); + public function __invoke(UserRegistered $event) + { $this->userFriendsRepository->add(new UserFriendsCollection($event->getUserId(), [])); $this->userCreditsRepository->add(new UserCredits($event->getUserId(), new Credits(0)); - } - - private function guard(Event $event) - { - if (false === ($event instanceof UserRegistered)) { - throw new \InvalidArgumentException("Expected event of type: ".UserRegistered::class); - } - } + } public static function subscribedTo() : string { diff --git a/src/CommandBus/CommandBus.php b/src/CommandBus/CommandBus.php index d9a09f8..bfbe14c 100644 --- a/src/CommandBus/CommandBus.php +++ b/src/CommandBus/CommandBus.php @@ -36,6 +36,7 @@ public function __invoke(Command $command, callable $next = null) if (empty($middleware) && !empty($current)) { $current->__invoke($command); + return; } diff --git a/src/CommandBus/Contracts/CommandHandler.php b/src/CommandBus/Contracts/CommandHandler.php index 28af619..aa2f634 100644 --- a/src/CommandBus/Contracts/CommandHandler.php +++ b/src/CommandBus/Contracts/CommandHandler.php @@ -17,5 +17,5 @@ interface CommandHandler /** * @param Command $command */ - public function __invoke(Command $command); + public function __invoke($command); } diff --git a/src/EventBus/Contracts/EventHandler.php b/src/EventBus/Contracts/EventHandler.php index 715417c..20ce652 100644 --- a/src/EventBus/Contracts/EventHandler.php +++ b/src/EventBus/Contracts/EventHandler.php @@ -20,9 +20,4 @@ interface EventHandler * @return string */ public static function subscribedTo() : string; - - /** - * @param Event $event - */ - public function __invoke(Event $event); } diff --git a/src/EventBus/EventBus.php b/src/EventBus/EventBus.php index b77c25f..e056872 100644 --- a/src/EventBus/EventBus.php +++ b/src/EventBus/EventBus.php @@ -39,6 +39,7 @@ public function __invoke(Event $event, callable $next = null) if (empty($middleware) && !empty($current)) { $current->__invoke($event); + return; } diff --git a/src/QueryBus/Contracts/QueryHandler.php b/src/QueryBus/Contracts/QueryHandler.php index a935098..9c9e79b 100644 --- a/src/QueryBus/Contracts/QueryHandler.php +++ b/src/QueryBus/Contracts/QueryHandler.php @@ -14,10 +14,4 @@ */ interface QueryHandler { - /** - * @param Query $query - * - * @return QueryResponse - */ - public function __invoke(Query $query) : QueryResponse; } diff --git a/tests/CommandBus/DummyCommand.php b/tests/CommandBus/DummyCommand.php index 6dfd328..00a1e4e 100644 --- a/tests/CommandBus/DummyCommand.php +++ b/tests/CommandBus/DummyCommand.php @@ -6,4 +6,7 @@ class DummyCommand implements Command { + public function __invoke() + { + } } diff --git a/tests/CommandBus/DummyCommandHandler.php b/tests/CommandBus/DummyCommandHandler.php index d04fbc7..5b994c3 100644 --- a/tests/CommandBus/DummyCommandHandler.php +++ b/tests/CommandBus/DummyCommandHandler.php @@ -2,15 +2,14 @@ namespace NilPortugues\Tests\MessageBus\CommandBus; -use NilPortugues\MessageBus\CommandBus\Contracts\Command; use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandler; class DummyCommandHandler implements CommandHandler { /** - * @param Command $command + * @param callable $command */ - public function __invoke(Command $command) + public function __invoke($command) { } } diff --git a/tests/EventBus/DummyEvent.php b/tests/EventBus/DummyEvent.php index 6756152..f362a35 100644 --- a/tests/EventBus/DummyEvent.php +++ b/tests/EventBus/DummyEvent.php @@ -6,4 +6,7 @@ class DummyEvent implements Event { + public function __invoke() + { + } } diff --git a/tests/EventBus/DummyEventHandler.php b/tests/EventBus/DummyEventHandler.php index 4ff52be..eb1dff2 100644 --- a/tests/EventBus/DummyEventHandler.php +++ b/tests/EventBus/DummyEventHandler.php @@ -11,7 +11,7 @@ class DummyEventHandler implements EventHandler, EventHandlerPriority /** * @param Event $event */ - public function __invoke(Event $event) + public function __invoke($event) { } diff --git a/tests/EventBus/SqliteEventHandler.php b/tests/EventBus/SqliteEventHandler.php index c8ac1d3..87e3028 100644 --- a/tests/EventBus/SqliteEventHandler.php +++ b/tests/EventBus/SqliteEventHandler.php @@ -16,11 +16,4 @@ public static function subscribedTo() : string { return SqliteEvent::class; } - - /** - * @param Event $event - */ - public function __invoke(Event $event) - { - } } diff --git a/tests/QueryBus/DummyQuery.php b/tests/QueryBus/DummyQuery.php index 84c49a1..afdcd8b 100644 --- a/tests/QueryBus/DummyQuery.php +++ b/tests/QueryBus/DummyQuery.php @@ -16,4 +16,7 @@ */ class DummyQuery implements Query { + public function __invoke() + { + } } diff --git a/tests/QueryBus/DummyQueryHandler.php b/tests/QueryBus/DummyQueryHandler.php index 17e0b36..9e9fcb6 100644 --- a/tests/QueryBus/DummyQueryHandler.php +++ b/tests/QueryBus/DummyQueryHandler.php @@ -23,7 +23,7 @@ class DummyQueryHandler implements QueryHandler * * @return QueryResponse */ - public function __invoke(Query $query) : QueryResponse + public function __invoke($query) : QueryResponse { return new DummyQueryResponse(); }