public static function getCommand($command_name, Context $context)
{
// it's oke to have no command, as there might be 'marker' states, where
// we just need to transition something to a next state (according to a
// rule)
// where useful work can be done (eg: from the 'initial' type state to
// a 'shortcut' state for special cases.
if ($command_name === '' || $command_name === null) {
// return a command without side effects
return new NullCommand();
}
$output = new Composite();
// a command string can be made up of multiple commands seperated by a
// comma
$all_commands = explode(',', $command_name);
// get the correct object to inject in the command(s)
$entity = $context->getEntity();
foreach ($all_commands as $single_command) {
if (!class_exists($single_command)) {
$e = new Exception(sprintf("failed command creation, class does not exist: (%s) for Context (%s)", $single_command, $context->toString()), Exception::COMMAND_CREATION_FAILURE);
throw $e;
}
try {
$command = new $single_command($entity);
$output->add($command);
} catch (\Exception $e) {
$e = new Exception(sprintf("command (%s) objects to construction for Context (%s). message: '%s'", $single_command, $context->toString(), $e->getMessage()), Exception::COMMAND_CREATION_FAILURE);
throw $e;
}
}
return $output;
}