The contract for classes that handle command execution in the cmd-bus system.
CommandHandlerInterface defines the standard interface for command handlers. These handlers contain the business logic for processing specific commands and are the final destination in the command processing pipeline.
interface CommandHandlerInterface
{
public function handle(CommandInterface $command): mixed;
}Processes a command and returns the result.
Parameters:
$command- The command to process
Returns:
mixed- The result of command processing (can be any type)
Command handlers serve as:
- Business Logic Container - Encapsulate the core business operations
- Single Responsibility - Each handler typically handles one command type
- Testable Units - Can be tested independently of the command bus
- Service Integration - Bridge between commands and domain services
Each handler should typically handle only one command type:
// ✅ Good - handles one command type
final class CreateUserHandler implements CommandHandlerInterface
{
public function handle(CommandInterface $command): User
{
assert($command instanceof CreateUserCommand);
// ...
}
}- CommandInterface - Commands that handlers process
- CommandHandlerMiddleware - Middleware that executes handlers
- CommandBusInterface - Interface that extends this one