Esempio n. 1
0
 public function doesRuleMatch(HeraldRule $rule, HeraldAdapter $object)
 {
     $phid = $rule->getPHID();
     if (isset($this->results[$phid])) {
         // If we've already evaluated this rule because another rule depends
         // on it, we don't need to reevaluate it.
         return $this->results[$phid];
     }
     if (isset($this->stack[$phid])) {
         // We've recursed, fail all of the rules on the stack. This happens when
         // there's a dependency cycle with "Rule conditions match for rule ..."
         // conditions.
         foreach ($this->stack as $rule_phid => $ignored) {
             $this->results[$rule_phid] = false;
         }
         throw new HeraldRecursiveConditionsException();
     }
     $this->stack[$phid] = true;
     $all = $rule->getMustMatchAll();
     $conditions = $rule->getConditions();
     $result = null;
     $local_version = id(new HeraldRule())->getConfigVersion();
     if ($rule->getConfigVersion() > $local_version) {
         $reason = pht('Rule could not be processed, it was created with a newer version ' . 'of Herald.');
         $result = false;
     } else {
         if (!$conditions) {
             $reason = pht('Rule failed automatically because it has no conditions.');
             $result = false;
         } else {
             if (!$rule->hasValidAuthor()) {
                 $reason = pht('Rule failed automatically because its owner is invalid ' . 'or disabled.');
                 $result = false;
             } else {
                 if (!$this->canAuthorViewObject($rule, $object)) {
                     $reason = pht('Rule failed automatically because it is a personal rule and its ' . 'owner can not see the object.');
                     $result = false;
                 } else {
                     if (!$this->canRuleApplyToObject($rule, $object)) {
                         $reason = pht('Rule failed automatically because it is an object rule which is ' . 'not relevant for this object.');
                         $result = false;
                     } else {
                         foreach ($conditions as $condition) {
                             $match = $this->doesConditionMatch($rule, $condition, $object);
                             if (!$all && $match) {
                                 $reason = 'Any condition matched.';
                                 $result = true;
                                 break;
                             }
                             if ($all && !$match) {
                                 $reason = 'Not all conditions matched.';
                                 $result = false;
                                 break;
                             }
                         }
                         if ($result === null) {
                             if ($all) {
                                 $reason = 'All conditions matched.';
                                 $result = true;
                             } else {
                                 $reason = 'No conditions matched.';
                                 $result = false;
                             }
                         }
                     }
                 }
             }
         }
     }
     $rule_transcript = new HeraldRuleTranscript();
     $rule_transcript->setRuleID($rule->getID());
     $rule_transcript->setResult($result);
     $rule_transcript->setReason($reason);
     $rule_transcript->setRuleName($rule->getName());
     $rule_transcript->setRuleOwner($rule->getAuthorPHID());
     $this->transcript->addRuleTranscript($rule_transcript);
     return $result;
 }
Esempio n. 2
0
 protected function doesRuleMatch(HeraldRule $rule, HeraldObjectAdapter $object)
 {
     $id = $rule->getID();
     if (isset($this->results[$id])) {
         // If we've already evaluated this rule because another rule depends
         // on it, we don't need to reevaluate it.
         return $this->results[$id];
     }
     if (isset($this->stack[$id])) {
         // We've recursed, fail all of the rules on the stack. This happens when
         // there's a dependency cycle with "Rule conditions match for rule ..."
         // conditions.
         foreach ($this->stack as $rule_id => $ignored) {
             $this->results[$rule_id] = false;
         }
         throw new HeraldRecursiveConditionsException();
     }
     $this->stack[$id] = true;
     $all = $rule->getMustMatchAll();
     $conditions = $rule->getConditions();
     $result = null;
     $local_version = id(new HeraldRule())->getConfigVersion();
     if ($rule->getConfigVersion() > $local_version) {
         $reason = "Rule could not be processed, it was created with a newer " . "version of Herald.";
         $result = false;
     } else {
         if (!$conditions) {
             $reason = "Rule failed automatically because it has no conditions.";
             $result = false;
         } else {
             if ($rule->hasInvalidOwner()) {
                 $reason = "Rule failed automatically because its owner is invalid " . "or disabled.";
                 $result = false;
             } else {
                 foreach ($conditions as $condition) {
                     $match = $this->doesConditionMatch($rule, $condition, $object);
                     if (!$all && $match) {
                         $reason = "Any condition matched.";
                         $result = true;
                         break;
                     }
                     if ($all && !$match) {
                         $reason = "Not all conditions matched.";
                         $result = false;
                         break;
                     }
                 }
                 if ($result === null) {
                     if ($all) {
                         $reason = "All conditions matched.";
                         $result = true;
                     } else {
                         $reason = "No conditions matched.";
                         $result = false;
                     }
                 }
             }
         }
     }
     $rule_transcript = new HeraldRuleTranscript();
     $rule_transcript->setRuleID($rule->getID());
     $rule_transcript->setResult($result);
     $rule_transcript->setReason($reason);
     $rule_transcript->setRuleName($rule->getName());
     $rule_transcript->setRuleOwner($rule->getAuthorPHID());
     $this->transcript->addRuleTranscript($rule_transcript);
     return $result;
 }