/** * Always returns an izzum exception (converts a non-izzum exception to an * izzum exception). * optionally throws it. * * @param \Exception $e * @param int $code * @return Exception * @throws Exception */ public static function wrapToStateMachineException(\Exception $e, $code, $throw = false) { if (!is_a($e, 'izzum\\statemachine\\Exception')) { // wrap the exception and use the code provided. $e = new Exception($e->getMessage(), $code, $e); } if ($throw) { throw $e; } return $e; }
/** * helper method * * @param ICommand $command * @throws Exception */ protected function execute(ICommand $command) { try { $command->execute(); } catch (\Exception $e) { // command failure $e = new Exception($e->getMessage(), Exception::COMMAND_EXECUTION_FAILURE, $e); throw $e; } }
/** * returns the associated Rule for this Transition, * configured with a 'reference' (stateful) object * * @param Context $context * the associated Context for a our statemachine * @return IRule a Rule or chained AndRule if the rule input was a ',' * seperated string of rules. * @throws Exception */ public function getRule(Context $context) { // if no rule is defined, just allow the transition by default if ($this->rule === '' || $this->rule === null) { return new TrueRule(); } $entity = $context->getEntity(); // a rule string can be made up of multiple rules seperated by a comma $all_rules = explode(',', $this->rule); $rule = new TrueRule(); foreach ($all_rules as $single_rule) { // guard clause to check if rule exists if (!class_exists($single_rule)) { $e = new Exception(sprintf("failed rule creation, class does not exist: (%s) for Context (%s).", $this->rule, $context->toString()), Exception::RULE_CREATION_FAILURE); throw $e; } try { $and_rule = new $single_rule($entity); // create a chain of rules that need to be true $rule = new AndRule($rule, $and_rule); } catch (\Exception $e) { $e = new Exception(sprintf("failed rule creation, class objects to construction with entity: (%s) for Context (%s). message: %s", $this->rule, $context->toString(), $e->getMessage()), Exception::RULE_CREATION_FAILURE); throw $e; } } return $rule; }