| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils\Response; |
| 4 |
|
| 5 |
/** |
| 6 |
* The typed result of one outbound cloud call (spec 043 / FR-003). |
| 7 |
* |
| 8 |
* `ResponseNormalizer` returns this instead of the historical |
| 9 |
* "array | WP_Error | [] | string" grab-bag. It is intentionally tiny: either it |
| 10 |
* carries a payload, or it carries a `TemplatelyError`. Never both, never |
| 11 |
* neither — that ambiguity (an empty array standing in for an error) is the |
| 12 |
* INV-2 bug this contract removes. |
| 13 |
*/ |
| 14 |
class RemoteResponse { |
| 15 |
|
| 16 |
/** |
| 17 |
* @var mixed |
| 18 |
*/ |
| 19 |
private $payload; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var TemplatelyError|null |
| 23 |
*/ |
| 24 |
private $error; |
| 25 |
|
| 26 |
/** |
| 27 |
* Raw HTTP status of the upstream response (0 when there was no response). |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
private $status; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param mixed $payload |
| 35 |
* @param TemplatelyError|null $error |
| 36 |
* @param int $status |
| 37 |
*/ |
| 38 |
private function __construct( $payload, $error, $status = 0 ) { |
| 39 |
$this->payload = $payload; |
| 40 |
$this->error = $error; |
| 41 |
$this->status = (int) $status; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param mixed $payload |
| 46 |
* @param int $status |
| 47 |
* @return RemoteResponse |
| 48 |
*/ |
| 49 |
public static function success( $payload, $status = 200 ) { |
| 50 |
return new self( $payload, null, $status ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param TemplatelyError $error |
| 55 |
* @param int $status Upstream HTTP status; defaults to the error's own. |
| 56 |
* @return RemoteResponse |
| 57 |
*/ |
| 58 |
public static function failure( TemplatelyError $error, $status = null ) { |
| 59 |
return new self( null, $error, null === $status ? $error->status() : $status ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
public function is_error() { |
| 66 |
return $this->error instanceof TemplatelyError; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* The successful payload, or `null` when this is an error. |
| 71 |
* |
| 72 |
* @return mixed |
| 73 |
*/ |
| 74 |
public function payload() { |
| 75 |
return $this->is_error() ? null : $this->payload; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @return TemplatelyError|null |
| 80 |
*/ |
| 81 |
public function error() { |
| 82 |
return $this->error; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @return int upstream HTTP status. |
| 87 |
*/ |
| 88 |
public function status() { |
| 89 |
return $this->status; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Back-compat bridge for call sites that still expect |
| 94 |
* `array|WP_Error` from the old helpers. New code should branch on |
| 95 |
* `is_error()` instead. |
| 96 |
* |
| 97 |
* @return mixed|TemplatelyError |
| 98 |
*/ |
| 99 |
public function unwrap() { |
| 100 |
return $this->is_error() ? $this->error : $this->payload; |
| 101 |
} |
| 102 |
} |
| 103 |
|