/** * Execute a command with validation and authorization. * * @param $command * @return mixed */ public function execute($command) { // If a validator is "registered," we will // first trigger it, before moving forward. $this->validateCommand($command); // Next, we'll execute any registered decorators. $this->executeDecorators($command, $this->decorators); // Check authorization if (Authority::cannot($command)) { throw new UnauthorizedHttpException('Unauthorized'); } $rules = [Authority::getLastEvaluator()->getEffectiveRules()]; // Execute authorization input decorators foreach ($rules as $rule) { if ($rule->getCondition()) { $this->executeDecorators($command, $rule->getCondition()->getInputDecorators()); } } // Pass through to the handler class. $command->result = $this->bus->execute($command); // Execute authorization output decorators foreach ($rules as $rule) { if ($rule->getCondition()) { $this->executeDecorators($command, $rule->getCondition()->getOutputDecorators()); } } return $command->result; }
function it_handles_command_if_validation_succeeds(Application $application, CommandTranslator $translator, CommandBus $bus, ExampleCommand $command, ExampleValidator $validator) { // Own responsibility $translator->toValidator($command)->willReturn(ExampleValidator::class); $application->make(ExampleValidator::class)->willReturn($validator); // Delegated responsibility $bus->execute($command)->shouldBeCalled(); $this->execute($command); }
/** * Execute a command with validation. * * @param $command * @return mixed */ public function execute($command) { // If a validator is "registered," we will // first trigger it, before moving forward. $this->validateCommand($command); // Next, we'll execute any registered decorators. $this->executeDecorators($command); // And finally pass through to the handler class. return $this->bus->execute($command); }
/** * redirect to a remote url * * @param $hash * @return \Illuminate\Http\RedirectResponse */ public function redirect($hash) { $url = $this->commandBus->execute(new GetUrlCommand($hash)); return \Redirect::to($url); }