tomwright/commander

There is no license information available for the latest version (1.0.2) of this package.

A simple command bus.

1.0.2 2016-03-24 09:52 UTC

This package is auto-updated.

Last update: 2024-11-15 07:11:25 UTC


README

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

Usage

You need a Command and a CommandHandler.

Let's say we have a command class stored in app/commanding/command/RegisterUserCommand.php.

namespace App\Commanding\Command;

class RegisterUserCommand extends \TomWright\Commander\Command\Command
{
	protected $username;
    protected $password;
    
    public function setUsername($username)
    {
    	$this->username = $username;
    }
    
    public function getUsername()
    {
    	return $this->username;
    }
    
    public function setPassword($password)
    {
        $this->password = $password;
    }
    
    public function getPassword()
    {
        return $this->password;
    }
}

Let's also assume we have a command handler class stored in app/commanding/handler/RegisterUserHandler.php.

namespace App\Commanding\Handler;

class RegisterUserHandler implements \TomWright\Commander\Handler\HandlerInterface
{   
    public function handle(\TomWright\Commander\Command\CommandInterface $command)
    {
    	echo "Registering user \"{$command->getUsername()}\" with password \"{$command->getPassword()}\".";
    }
}

Now we need to add a Command Handler namespace so as the CommandBus knows where to look for the handlers.

$bus = \TomWright\Commander\CommandBus::getInstance();
$bus->addHandlerNamespace('\\App\\Commanding\\Handler');

Now whenever we want to register a new user, all we have to do is the following:

$bus = \TomWright\Commander\CommandBus::getInstance();
$command = new \App\Commanding\Command\RegisterUserCommand();
$command->setUsername('Some user');
$command->setPassword('Somepassword123');
$bus->handle($command);

Remember - a Command is an action and not a notification. If you are looking for a notification/event handler see TomWright/Eventing.