/** * Execute the handler * * @param CommandInterface $command The command * @param CommandChainInterface $chain The chain executing the command * @return mixed|null If a handler breaks, returns the break condition. NULL otherwise. */ public function execute(CommandInterface $command, CommandChainInterface $chain) { $parts = explode('.', $command->getName()); $method = '_' . $parts[0] . ucfirst($parts[1]); if (method_exists($this, $method)) { return $this->{$method}($command); } }
/** * @param MessageInterface|CommandInterface $message * @return CommandHandlerInterface * @throws CommandBusException */ protected function getHandler(CommandInterface $message) { $messageName = $message->getName(); $handlerId = $this->resolver->resolve($messageName); if (!$this->locator->has($handlerId)) { throw new CommandBusException(sprintf("Cannot instantiate handler '%s' for command '%s'", $handlerId, $messageName)); } /** @var CommandHandlerInterface $handler */ $handler = $this->locator->get($handlerId); if (!$handler instanceof CommandHandlerInterface) { throw new CommandBusException(sprintf("Handler '%s' returned by locator for command '%s' should implement CommandHandlerInterface", $handlerId, $messageName)); } return $handler; }
/** * Command handler * * Only handles before.action commands to check authorization rules. * * @param CommandInterface $command The command * @param CommandChainInterface $chain The chain executing the command * @throws ControllerExceptionRequestForbidden If the user is authentic and the actions is not allowed. * @throws ControllerExceptionRequestNotAuthorized If the user is not authentic and the action is not allowed. * @return boolean Return TRUE if action is permitted. FALSE otherwise. */ public function execute(CommandInterface $command, CommandChainInterface $chain) { $parts = explode('.', $command->getName()); if ($parts[0] == 'before') { $action = $parts[1]; if ($this->canExecute($action) === false) { $message = 'Action ' . ucfirst($action) . ' Not Allowed'; if ($this->getUser()->isAuthentic()) { if (!$this->getUser()->isEnabled()) { $message = 'Account disabled'; } throw new ControllerExceptionRequestForbidden($message); } else { throw new ControllerExceptionRequestNotAuthorized($message); } return false; } } return true; }
/** * Command handler * * @param CommandInterface $command The command * @param CommandChainInterface $chain The chain executing the command * @return mixed|null If a handler breaks, returns the break condition. NULL otherwise. */ public function execute(CommandInterface $command, CommandChainInterface $chain) { $type = ''; $package = ''; $subject = ''; if ($command->getSubject()) { $identifier = $command->getSubject()->getIdentifier()->toArray(); $package = $identifier['package']; if ($identifier['path']) { $type = array_shift($identifier['path']); $subject = $identifier['name']; } else { $type = $identifier['name']; } } $parts = explode('.', $command->getName()); $when = array_shift($parts); // Before or After $name = StringInflector::implode($parts); // Action // Create Specific and Generic event names $event_specific = 'on' . ucfirst($when) . ucfirst($package) . ucfirst($subject) . ucfirst($type) . $name; $event_generic = 'on' . ucfirst($when) . ucfirst($type) . $name; // Clone the context if ($this->isEventImmutable()) { $event = clone $command; } else { $event = $command; } // Create event object to check for propagation $event = $this->getEventPublisher()->publishEvent($event_specific, $event->getAttributes(), $event->getSubject()); // Ensure event can be propagated and event name is different if ($event->canPropagate() && $event_specific != $event_generic) { $event->setName($event_generic); $this->getEventPublisher()->publishEvent($event); } }