| 1 | <?php |
||
| 10 | abstract class Result { |
||
| 11 | /** |
||
| 12 | * @var int |
||
| 13 | */ |
||
| 14 | private $httpCode; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $response; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param int $httpCode |
||
| 23 | * @param $response |
||
| 24 | */ |
||
| 25 | public function __construct($httpCode, $response) { |
||
| 26 | $this->httpCode = $httpCode; |
||
| 27 | $this->processResponse($response); |
||
| 28 | $this->response = $response; |
||
| 29 | } |
||
| 30 | |||
| 31 | final public function isSuccess() { |
||
| 32 | return $this->httpCode == 200; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return null|int |
||
| 37 | */ |
||
| 38 | final public function getErrorCode() { |
||
| 39 | if(false === $this->isSuccess()) { |
||
| 40 | return $this->httpCode; |
||
| 41 | } |
||
| 42 | return null; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | final public function getErrorMessage() { |
||
| 49 | return $this->getErrorMessageByErrorCode($this->getErrorCode()); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | final public function getRawResponse() { |
||
| 56 | return $this->response; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param int $httpCode |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | abstract protected function getErrorMessageByErrorCode($httpCode); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $response |
||
| 67 | */ |
||
| 68 | abstract protected function processResponse($response); |
||
| 69 | } |
||
| 70 |