Created
October 18, 2014 13:06
-
-
Save MarkRedeman/04545818fa2dd32dc72d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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