1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 14:52:41 +01:00

Lift core of "HeraldConditionResult" to "HeraldTranscriptResult"

Summary: Ref T13586. Lift the behavioral core of "HeraldConditionResult" into a new abstract base "HeraldTranscriptResult", with the intent to introduce a "HeraldRuleResult".

Test Plan:
  - Ran Herald rules, reviewed transcripts.
  - This change should have no behavioral effect.

Maniphest Tasks: T13586

Differential Revision: https://secure.phabricator.com/D21564
This commit is contained in:
epriestley 2021-02-18 09:35:12 -08:00
parent e77ae13d5c
commit b047653e53
4 changed files with 93 additions and 78 deletions

View file

@ -1662,6 +1662,7 @@ phutil_register_library_map(array(
'HeraldTranscriptListController' => 'applications/herald/controller/HeraldTranscriptListController.php',
'HeraldTranscriptPHIDType' => 'applications/herald/phid/HeraldTranscriptPHIDType.php',
'HeraldTranscriptQuery' => 'applications/herald/query/HeraldTranscriptQuery.php',
'HeraldTranscriptResult' => 'applications/herald/storage/transcript/HeraldTranscriptResult.php',
'HeraldTranscriptSearchEngine' => 'applications/herald/query/HeraldTranscriptSearchEngine.php',
'HeraldTranscriptTestCase' => 'applications/herald/storage/__tests__/HeraldTranscriptTestCase.php',
'HeraldUtilityActionGroup' => 'applications/herald/action/HeraldUtilityActionGroup.php',
@ -7794,7 +7795,7 @@ phutil_register_library_map(array(
'HarbormasterBuildableAdapterInterface',
),
'HeraldCondition' => 'HeraldDAO',
'HeraldConditionResult' => 'Phobject',
'HeraldConditionResult' => 'HeraldTranscriptResult',
'HeraldConditionTranscript' => 'Phobject',
'HeraldContentSourceField' => 'HeraldField',
'HeraldController' => 'PhabricatorController',
@ -7905,6 +7906,7 @@ phutil_register_library_map(array(
'HeraldTranscriptListController' => 'HeraldController',
'HeraldTranscriptPHIDType' => 'PhabricatorPHIDType',
'HeraldTranscriptQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'HeraldTranscriptResult' => 'Phobject',
'HeraldTranscriptSearchEngine' => 'PhabricatorApplicationSearchEngine',
'HeraldTranscriptTestCase' => 'PhabricatorTestCase',
'HeraldUtilityActionGroup' => 'HeraldActionGroup',

View file

@ -1,7 +1,7 @@
<?php
final class HeraldConditionResult
extends Phobject {
extends HeraldTranscriptResult {
const RESULT_MATCHED = 'matched';
const RESULT_FAILED = 'failed';
@ -10,61 +10,20 @@ final class HeraldConditionResult
const RESULT_EXCEPTION = 'exception';
const RESULT_UNKNOWN = 'unknown';
private $resultCode;
private $resultData = array();
public function toMap() {
return array(
'code' => $this->getResultCode(),
'data' => $this->getResultData(),
);
}
public static function newFromMap(array $map) {
$result_code = idx($map, 'code');
$result = self::newFromResultCode($result_code);
$result_data = idx($map, 'data', array());
$result->setResultData($result_data);
return $result;
}
public static function newFromResultCode($result_code) {
$map = self::getResultSpecification($result_code);
$result = new self();
$result->resultCode = $result_code;
return $result;
return id(new self())->setResultCode($result_code);
}
public function getResultCode() {
return $this->resultCode;
}
private function getResultData() {
return $this->resultData;
}
public function getIconIcon() {
return $this->getSpecificationProperty('icon');
}
public function getIconColor() {
return $this->getSpecificationProperty('color.icon');
public static function newFromResultMap(array $map) {
return id(new self())->loadFromResultMap($map);
}
public function getIsMatch() {
return ($this->getSpecificationProperty('match') === true);
}
public function getName() {
return $this->getSpecificationProperty('name');
}
public function newDetailsView() {
switch ($this->resultCode) {
switch ($this->getResultCode()) {
case self::RESULT_OBJECT_STATE:
$reason = $this->getDataProperty('reason');
$details = HeraldStateReasons::getExplanation($reason);
@ -105,35 +64,7 @@ final class HeraldConditionResult
return $details;
}
public function setResultData(array $result_data) {
$this->resultData = $result_data;
return $this;
}
private function getDataProperty($key) {
$data = $this->getResultData();
return idx($data, $key);
}
private function getSpecificationProperty($key) {
$map = self::getResultSpecification($this->resultCode);
return $map[$key];
}
private static function getResultSpecification($result_code) {
$map = self::getResultSpecificationMap();
if (!isset($map[$result_code])) {
throw new Exception(
pht(
'Condition result "%s" is unknown.',
$result_code));
}
return $map[$result_code];
}
private static function getResultSpecificationMap() {
protected function newResultSpecificationMap() {
return array(
self::RESULT_MATCHED => array(
'match' => true,

View file

@ -65,7 +65,7 @@ final class HeraldConditionTranscript extends Phobject {
}
public function setResult(HeraldConditionResult $result) {
$this->resultMap = $result->toMap();
$this->resultMap = $result->newResultMap();
return $this;
}
@ -73,7 +73,7 @@ final class HeraldConditionTranscript extends Phobject {
$map = $this->resultMap;
if (is_array($map)) {
$result = HeraldConditionResult::newFromMap($map);
$result = HeraldConditionResult::newFromResultMap($map);
} else {
$legacy_result = $this->result;

View file

@ -0,0 +1,82 @@
<?php
abstract class HeraldTranscriptResult
extends Phobject {
private $resultCode;
private $resultData = array();
final protected function setResultCode($result_code) {
$this->resultCode = $result_code;
return $this;
}
final protected function loadFromResultMap(array $map) {
$result_code = idx($map, 'code');
$result_data = idx($map, 'data', array());
$this
->setResultCode($result_code)
->setResultData($result_data);
return $this;
}
final public function getResultCode() {
return $this->resultCode;
}
final protected function getResultData() {
return $this->resultData;
}
final public function setResultData(array $result_data) {
$this->resultData = $result_data;
return $this;
}
final public function getIconIcon() {
return $this->getSpecificationProperty('icon');
}
final public function getIconColor() {
return $this->getSpecificationProperty('color.icon');
}
final public function getName() {
return $this->getSpecificationProperty('name');
}
final protected function getDataProperty($key) {
$data = $this->getResultData();
return idx($data, $key);
}
final public function newResultMap() {
return array(
'code' => $this->getResultCode(),
'data' => $this->getResultData(),
);
}
final protected function getSpecificationProperty($key) {
$map = $this->getResultSpecification($this->getResultCode());
return $map[$key];
}
final protected function getResultSpecification($result_code) {
$map = $this->newResultSpecificationMap();
if (!isset($map[$result_code])) {
throw new Exception(
pht(
'Result code "%s" is unknown.',
$result_code));
}
return $map[$result_code];
}
abstract protected function newResultSpecificationMap();
}