Simple command bus implementation
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
.gitignore
.travis.yml
Command.php
CommandBus.php
CommandHandlerInterface.php
CommandHandlerNotFoundException.php
CommandInterface.php
CommandMiddlewareInterface.php
README.md
composer.json
composer.lock
phpunit.xml.dist

README.md

Command Bus

Define a simple command

  1. Create new class extends from go1\command\Command
namespace devel;

use go1\command\Command;

class CommandFoo extends Command 
{
    # Inject dependencices
    public function __constructor() 
    {
    }
    
    public function execute()
    {
        $payload = $this->getPayload();
        // Do your bussiness
    }
} 
  1. Run CommandFoo via CommandBus
namespace devel;

use go1\command\CommandBus;

$commandFoo = new CommandFoo();
$commandFoo->setPayload($data = 'your data');

$commandBus = new CommandBus();
$commandBus->execute($commandFoo);

Define a command with handler

If your logic quite complex and you want to move it to a handler service, you can create new handler class and attach to command bus.

Note: CommandBus auto maps your command & handler. Following naming pattern, if command is CommandFoo, handler must be CommandFooHandler

  1. Create new class extends from go1\command\Command
namespace devel;

use go1\command\Command;

class CommandFoo extends Command 
{
    # Now command only contains data
} 
  1. Create new class implements from go1\command\CommandHandlerInterface
 namespace devel;
 
 use go1\command\CommandHandlerInterface;
 
 class CommandFooHandler implements CommandHandlerInterface 
 {
     # Inject dependencices
     public function __constructor() 
     {
     }
     
     public function handle(CommandInterface $command) 
     {
       # Do your bussiness here
     }
 } 
 
  1. Run command via CommandBus
namespace devel;

use go1\command\CommandBus;
use go1\command\CommandFooHandler;
use go1\command\CommandFoo;

$commandFoo = new CommandFoo();
$commandFoo->setPayload($data = 'your data');

$commandFooHandler = new CommandFooHandler();

$commandBus = new CommandBus([$commandFooHandler]);
$commandBus->execute($commandFoo);

Attach middleware to CommandBus

  1. Create new class implements from go1\command\CommandMiddlewareInterface
namespace devel;
  
use go1\command\CommandMiddlewareInterface;

class CommandFooHandler implements CommandMiddlewareInterface 
{
    # Inject dependencices
    public function __constructor() 
    {
    }
    
    public function invoke(CommandInterface $command) 
    {
        # Do your bussiness here
        
        # If you can interrups command by:
        $command->stopPropagation();     
    }
  
}  
  1. Attach middleware to CommandBus
namespace devel;

use go1\command\CommandBus;
use go1\command\CommandFooHandler;
use go1\command\CommandFoo;

$commandFoo = new CommandFoo();
$commandFoo->setPayload($data = 'your data');

$commandFooHandler = new CommandFooHandler();
$commandFooMiddleware = new CommandFooMiddleware()

$commandBus = new CommandBus([$commandFooHandler], [$commandFooMiddleware]);
$commandBus->execute($commandFoo);