Skip to content

Instantly share code, notes, and snippets.

@MarkRedeman
Created October 18, 2014 13:06
Show Gist options
  • Save MarkRedeman/04545818fa2dd32dc72d to your computer and use it in GitHub Desktop.
Save MarkRedeman/04545818fa2dd32dc72d to your computer and use it in GitHub Desktop.
<?php namespace Reconcept\Core\Commands;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
class DefaultCommandBus implements CommandBus {
/**
* @var Application
*/
protected $app;
/**
* @var CommandTranslator
*/
protected $commandTranslator;
/**
* @param Application $app
* @param CommandTranslator $commandTranslator
*/
function __construct(Application $app, CommandTranslator $commandTranslator)
{
$this->app = $app;
$this->commandTranslator = $commandTranslator;
}
/**
* Decorate the command bus with any executable actions.
*
* @param string $className
* @return mixed
*/
public function decorate($className)
{
$this->decorators[] = $className;
}
/**
* Execute the command
*
* @param $command
* @return mixed
*/
public function execute($command)
{
$handler = $this->commandTranslator->toCommandHandler($command);
return $this->app->make($handler)->handle($command);
}
}
class ChangeQualificationCommand implements Command {
public $qualification_id;
public function __construct($qualification_id)
{
# code...
}
}
class ChangeQualificationCommandSanitizerDecorator extends BaseCommandSanitizerDecorator implements CommandHandler {
protected $decorated;
// de handle en constructor method kan ook in een BaseCommandSanitizerDecorator class zitten
public function __construct(CommandHandler $decorater)
{
$this->decorated = $decorater;
}
public function handle(ChangeQualificationCommand $command)
{
$sanitizedCommand = $this->sanitize($command);
$this->decorated->handle($command);
}
private function sanitize(ChangeQualificationCommand $command)
{
// slecht sanitize voorbeeld
$command->qualification_id = (int) $qualification_id;
}
}
class ChangeQualificationCommandValidatorDecorator implements CommandHandler {
protected $decorated;
// de handle en constructor method kan ook in een BaseCommandSanitizerDecorator class zitten
public function __construct(CommandHandler $decorater)
{
$this->decorated = $decorater;
}
public function handle(ChangeQualificationCommand $command)
{
$this->validate($command);
$this->decorated->handle($command);
}
private function validate($command)
{
}
}
class ChangeQualificationCommandAuthorizerDecorator implements CommandHandler {
protected $decorated;
// de handle en constructor method kan ook in een BaseCommandSanitizerDecorator class zitten
public function __construct(CommandHandler $decorater)
{
$this->decorated = $decorater;
}
public function handle(ChangeQualificationCommand $command)
{
$this->authorize($command);
$this->decorated->handle($command);
}
private function authorize($command)
{
}
}
class ChangeQualificationCommandHandler implements CommandHandler {
protected $repo;
public function __construct(Repository $repo)
{
$repo = $repo;
}
public function handle(ChangeQualificationCommand $command)
{
// handle stuff
}
}
class QualificationServiceProvider {
public function register()
{
$this->app->bindShared('ChangeQualificationCommandHandler', function() {
return new ChangeQualificationCommandSanitizerDecorator(
new ChangeQualificationCommandValidatorDecorator(
new ChangeQualificationCommandAuthorizerDecorator(
$this->app->make(ChangeQualificationCommandValidatorHandler::class)
)
)
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment