Changeset View
Changeset View
Standalone View
Standalone View
src/applications/herald/storage/transcript/HeraldConditionResult.php
<?php | <?php | ||||
final class HeraldConditionResult | final class HeraldConditionResult | ||||
extends Phobject { | extends HeraldTranscriptResult { | ||||
const RESULT_MATCHED = 'matched'; | const RESULT_MATCHED = 'matched'; | ||||
const RESULT_FAILED = 'failed'; | const RESULT_FAILED = 'failed'; | ||||
const RESULT_OBJECT_STATE = 'object-state'; | const RESULT_OBJECT_STATE = 'object-state'; | ||||
const RESULT_INVALID = 'invalid'; | const RESULT_INVALID = 'invalid'; | ||||
const RESULT_EXCEPTION = 'exception'; | const RESULT_EXCEPTION = 'exception'; | ||||
const RESULT_UNKNOWN = 'unknown'; | 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) { | public static function newFromResultCode($result_code) { | ||||
$map = self::getResultSpecification($result_code); | return id(new self())->setResultCode($result_code); | ||||
$result = new self(); | |||||
$result->resultCode = $result_code; | |||||
return $result; | |||||
} | |||||
public function getResultCode() { | |||||
return $this->resultCode; | |||||
} | |||||
private function getResultData() { | |||||
return $this->resultData; | |||||
} | } | ||||
public function getIconIcon() { | public static function newFromResultMap(array $map) { | ||||
return $this->getSpecificationProperty('icon'); | return id(new self())->loadFromResultMap($map); | ||||
} | |||||
public function getIconColor() { | |||||
return $this->getSpecificationProperty('color.icon'); | |||||
} | } | ||||
public function getIsMatch() { | public function getIsMatch() { | ||||
return ($this->getSpecificationProperty('match') === true); | return ($this->getSpecificationProperty('match') === true); | ||||
} | } | ||||
public function getName() { | |||||
return $this->getSpecificationProperty('name'); | |||||
} | |||||
public function newDetailsView() { | public function newDetailsView() { | ||||
switch ($this->resultCode) { | switch ($this->getResultCode()) { | ||||
case self::RESULT_OBJECT_STATE: | case self::RESULT_OBJECT_STATE: | ||||
$reason = $this->getDataProperty('reason'); | $reason = $this->getDataProperty('reason'); | ||||
$details = HeraldStateReasons::getExplanation($reason); | $details = HeraldStateReasons::getExplanation($reason); | ||||
break; | break; | ||||
case self::RESULT_INVALID: | case self::RESULT_INVALID: | ||||
case self::RESULT_EXCEPTION: | case self::RESULT_EXCEPTION: | ||||
$error_class = $this->getDataProperty('exception.class'); | $error_class = $this->getDataProperty('exception.class'); | ||||
$error_message = $this->getDataProperty('exception.message'); | $error_message = $this->getDataProperty('exception.message'); | ||||
Show All 24 Lines | switch ($this->getResultCode()) { | ||||
default: | default: | ||||
$details = null; | $details = null; | ||||
break; | break; | ||||
} | } | ||||
return $details; | return $details; | ||||
} | } | ||||
public function setResultData(array $result_data) { | protected function newResultSpecificationMap() { | ||||
$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() { | |||||
return array( | return array( | ||||
self::RESULT_MATCHED => array( | self::RESULT_MATCHED => array( | ||||
'match' => true, | 'match' => true, | ||||
'icon' => 'fa-check', | 'icon' => 'fa-check', | ||||
'color.icon' => 'green', | 'color.icon' => 'green', | ||||
'name' => pht('Passed'), | 'name' => pht('Passed'), | ||||
), | ), | ||||
self::RESULT_FAILED => array( | self::RESULT_FAILED => array( | ||||
Show All 33 Lines |