Skip to content

Latest commit

 

History

History

Command

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Command Design Pattern

The command design pattern is used to encapsulate the parameters of a request. Only scalar types (int, string, etc.) and DTO's are passed to the constructor of this simple data transfer object.

Below we use the ChangePasswordCommand and ChangePasswordHandler to facilitate the call to the UserService changePassword() method. This enables us to later use the Decorator pattern on the CommandBusInterface to add behavior for audit logging, throttling actions, etc.

Also Known As

  • Action Design Pattern
  • Transaction Design Pattern

Example

Command:

final class ChangePasswordCommand implements CommandInterface
{
    public function __construct($userId, $password)
    {
        $this->userId = (int) $userId;
        $this->password = (string) $password;
    }
}

Handler:

final class ChangePasswordHandler
{
    public function handle(ChangePasswordCommand $command)
    {
        $this->userService->changePassword(
            $command->getUserId(),
            $command->getPassword()
        );
    }
}

Executing the command:

$userId = 1;
$newPassword = 'newpassword123';
$this->dispatch(new ChangePasswordCommand($userId, $newPassword));