proyectotau/command-bus

Minimalist implementation of CommandBus pattern for test purpose only

1.2.0 2022-03-21 21:10 UTC

This package is auto-updated.

Last update: 2024-09-22 02:43:52 UTC


README

Minimalist implementation of CommandBus pattern for test purpose only

According to (it remembers remotelly to)

https://tactician.thephpleague.com/

Install

Insert official repository in your composer.json

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/proyectotau/command-bus.git"
        }
    ],

Require it by composer

   composer -vvv require proyectotau/command-bus:dev-master

Usage

Create a CommandHandler class

class CommandHandler {
	
	function handler($cmd){
		// run your task here
		// you can get public var from cmd if it is an object
	}
}

Make a CommandBus class

	$cmdbus = new CommandBus();

And bind command to that command handler

	$cmdbus->bind('MyCommand', $handler);

Command can be an object with parameters

	$cmdobj = new CommandObject(true, 1, []);
	$cmdbus->bind($cmdobj, $handler);

Finally, dispatch command

	$cmdbus->dispatch('MyCommand');

or

	$cmdbus->dispatch($cmdobj);

As a result, handler method will be invoke receiving command as an argument. If it is an object, you could get constructor's params. Let command be an object like this:

class CommandObject {
	public $param1;
	public $param2;
	public $param3;
	
	function __constructor($param1, $param2, $param3) {
		$this->param1 = $param1;
		$this->param2 = $param2;
		$this->param3 = $param3;
	}
}

You can pick up them

function handler($cmd){
		$this->param1 = $cmd->param1;
		$this->param2 = $cmd->param2;
		$this->param3 = $cmd->param3;
	}

Tests

You can run tests like this

vendor/bin/phpunit --color --testdox tests/CommandBusTest.php