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;
}