Skip to content

Commit

Permalink
Ditching type hinting for handlers and updating README
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Apr 23, 2016
1 parent f9ba1a8 commit 8122e51
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 66 deletions.
52 changes: 10 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,16 @@ 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(),
$command->getEmail(),
);

$this->userRepository->save($user);
}

private function guard(Command $command)
{
if (false === ($command instanceof RegisterUser)) {
throw new \InvalidArgumentException("Expected command of type: ".RegisterUser::class);
}
}
}
}
```

Expand Down Expand Up @@ -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);
}
}
}
```

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/CommandBus/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function __invoke(Command $command, callable $next = null)

if (empty($middleware) && !empty($current)) {
$current->__invoke($command);

return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/CommandBus/Contracts/CommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ interface CommandHandler
/**
* @param Command $command
*/
public function __invoke(Command $command);
public function __invoke($command);
}
5 changes: 0 additions & 5 deletions src/EventBus/Contracts/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,4 @@ interface EventHandler
* @return string
*/
public static function subscribedTo() : string;

/**
* @param Event $event
*/
public function __invoke(Event $event);
}
1 change: 1 addition & 0 deletions src/EventBus/EventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __invoke(Event $event, callable $next = null)

if (empty($middleware) && !empty($current)) {
$current->__invoke($event);

return;
}

Expand Down
6 changes: 0 additions & 6 deletions src/QueryBus/Contracts/QueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@
*/
interface QueryHandler
{
/**
* @param Query $query
*
* @return QueryResponse
*/
public function __invoke(Query $query) : QueryResponse;
}
3 changes: 3 additions & 0 deletions tests/CommandBus/DummyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

class DummyCommand implements Command
{
public function __invoke()
{
}
}
5 changes: 2 additions & 3 deletions tests/CommandBus/DummyCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
}
3 changes: 3 additions & 0 deletions tests/EventBus/DummyEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

class DummyEvent implements Event
{
public function __invoke()
{
}
}
2 changes: 1 addition & 1 deletion tests/EventBus/DummyEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DummyEventHandler implements EventHandler, EventHandlerPriority
/**
* @param Event $event
*/
public function __invoke(Event $event)
public function __invoke($event)
{
}

Expand Down
7 changes: 0 additions & 7 deletions tests/EventBus/SqliteEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,4 @@ public static function subscribedTo() : string
{
return SqliteEvent::class;
}

/**
* @param Event $event
*/
public function __invoke(Event $event)
{
}
}
3 changes: 3 additions & 0 deletions tests/QueryBus/DummyQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
*/
class DummyQuery implements Query
{
public function __invoke()
{
}
}
2 changes: 1 addition & 1 deletion tests/QueryBus/DummyQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DummyQueryHandler implements QueryHandler
*
* @return QueryResponse
*/
public function __invoke(Query $query) : QueryResponse
public function __invoke($query) : QueryResponse
{
return new DummyQueryResponse();
}
Expand Down

0 comments on commit 8122e51

Please sign in to comment.