Skip to content

Commit 8122e51

Browse files
committed
Ditching type hinting for handlers and updating README
1 parent f9ba1a8 commit 8122e51

13 files changed

Lines changed: 26 additions & 66 deletions

File tree

README.md

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,16 @@ final class RegisterUserHandler implements CommandHandler
140140
$this->userRepository = $userRepository;
141141
}
142142

143-
public function __invoke(Command $command)
144-
{
145-
$this->guard($command);
146-
143+
public function __invoke(RegisterUser $command)
144+
{
147145
$user = new User(
148146
$command->getUsername(),
149147
$command->getPassword(),
150148
$command->getEmail(),
151149
);
152150

153151
$this->userRepository->save($user);
154-
}
155-
156-
private function guard(Command $command)
157-
{
158-
if (false === ($command instanceof RegisterUser)) {
159-
throw new \InvalidArgumentException("Expected command of type: ".RegisterUser::class);
160-
}
161-
}
152+
}
162153
}
163154
```
164155

@@ -328,21 +319,13 @@ final class GetUserHandler implements QueryHandler
328319
$this->userRepository = $userRepository;
329320
}
330321

331-
public function __invoke(Query $query) : QueryResponse
332-
{
333-
$this->guard($query);
322+
public function __invoke(GetUser $query) : QueryResponse
323+
{
334324
$userId = $query->getUserId();
335325
$user = $this->userRepository->find($userId);
336326

337327
return new UserQueryResponse($user);
338328
}
339-
340-
private function guard(Query $query)
341-
{
342-
if (false === ($query instanceof GetUser)) {
343-
throw new \InvalidArgumentException("Expected query of type: ".GetUser::class);
344-
}
345-
}
346329
}
347330
```
348331

@@ -543,19 +526,12 @@ final class SendWelcomeEmailHandler implements EventHandler
543526
$this->emailProvider = $emailProvider;
544527
}
545528

546-
public function __invoke(Event $event)
529+
public function __invoke(UserRegistered $event)
547530
{
548531
$this->guard($event);
549532
$this->emailProvider->send('welcome_email', $event->getEmail());
550533
}
551-
552-
private function guard(Event $event)
553-
{
554-
if (false === ($event instanceof UserRegistered)) {
555-
throw new \InvalidArgumentException("Expected event of type: ".UserRegistered::class);
556-
}
557-
}
558-
534+
559535
public static function subscribedTo() : string
560536
{
561537
return UserRegistered::class;
@@ -583,19 +559,11 @@ final class SetupUserAccountHandler implements EventHandler
583559
$this->userCreditsRepository = $userCreditsRepository;
584560
}
585561

586-
public function __invoke(Event $event)
587-
{
588-
$this->guard($event);
562+
public function __invoke(UserRegistered $event)
563+
{
589564
$this->userFriendsRepository->add(new UserFriendsCollection($event->getUserId(), []));
590565
$this->userCreditsRepository->add(new UserCredits($event->getUserId(), new Credits(0));
591-
}
592-
593-
private function guard(Event $event)
594-
{
595-
if (false === ($event instanceof UserRegistered)) {
596-
throw new \InvalidArgumentException("Expected event of type: ".UserRegistered::class);
597-
}
598-
}
566+
}
599567

600568
public static function subscribedTo() : string
601569
{

src/CommandBus/CommandBus.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __invoke(Command $command, callable $next = null)
3636

3737
if (empty($middleware) && !empty($current)) {
3838
$current->__invoke($command);
39+
3940
return;
4041
}
4142

src/CommandBus/Contracts/CommandHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ interface CommandHandler
1717
/**
1818
* @param Command $command
1919
*/
20-
public function __invoke(Command $command);
20+
public function __invoke($command);
2121
}

src/EventBus/Contracts/EventHandler.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,4 @@ interface EventHandler
2020
* @return string
2121
*/
2222
public static function subscribedTo() : string;
23-
24-
/**
25-
* @param Event $event
26-
*/
27-
public function __invoke(Event $event);
2823
}

src/EventBus/EventBus.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __invoke(Event $event, callable $next = null)
3939

4040
if (empty($middleware) && !empty($current)) {
4141
$current->__invoke($event);
42+
4243
return;
4344
}
4445

src/QueryBus/Contracts/QueryHandler.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,4 @@
1414
*/
1515
interface QueryHandler
1616
{
17-
/**
18-
* @param Query $query
19-
*
20-
* @return QueryResponse
21-
*/
22-
public function __invoke(Query $query) : QueryResponse;
2317
}

tests/CommandBus/DummyCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66

77
class DummyCommand implements Command
88
{
9+
public function __invoke()
10+
{
11+
}
912
}

tests/CommandBus/DummyCommandHandler.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace NilPortugues\Tests\MessageBus\CommandBus;
44

5-
use NilPortugues\MessageBus\CommandBus\Contracts\Command;
65
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandler;
76

87
class DummyCommandHandler implements CommandHandler
98
{
109
/**
11-
* @param Command $command
10+
* @param callable $command
1211
*/
13-
public function __invoke(Command $command)
12+
public function __invoke($command)
1413
{
1514
}
1615
}

tests/EventBus/DummyEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66

77
class DummyEvent implements Event
88
{
9+
public function __invoke()
10+
{
11+
}
912
}

tests/EventBus/DummyEventHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DummyEventHandler implements EventHandler, EventHandlerPriority
1111
/**
1212
* @param Event $event
1313
*/
14-
public function __invoke(Event $event)
14+
public function __invoke($event)
1515
{
1616
}
1717

0 commit comments

Comments
 (0)