ActivationForm.php
2 years ago
CheckForm.php
2 years ago
Endpoint.php
4 years ago
License.php
2 years ago
RequestResponse.php
4 years ago
Updater.php
4 years ago
RequestResponse.php
105 lines
| 1 | <?php |
| 2 | namespace Kubio\Core\License; |
| 3 | |
| 4 | class RequestResponse { |
| 5 | |
| 6 | private $response; |
| 7 | private $response_body; |
| 8 | private $response_code; |
| 9 | |
| 10 | public function __construct( $response ) { |
| 11 | $this->response = $response; |
| 12 | $this->response_body = json_decode( wp_remote_retrieve_body( $this->response ) ); |
| 13 | |
| 14 | $this->response_code = wp_remote_retrieve_response_code( $this->response ); |
| 15 | |
| 16 | if ( ! $this->response_body ) { |
| 17 | $this->response_body = new \stdClass(); |
| 18 | $this->response_body->errors = array( |
| 19 | 'body' => wp_remote_retrieve_body( $this->response ), |
| 20 | ); |
| 21 | $this->response_body->status = 'error'; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public function getMessage( $implode = false ) { |
| 26 | $message = array(); |
| 27 | if ( $this->isWPError() ) { |
| 28 | $message = $this->getWPError(); |
| 29 | } else { |
| 30 | if ( $this->isSuccess() ) { |
| 31 | $message = $this->getResponseBody()->body; |
| 32 | |
| 33 | } else { |
| 34 | $message = $this->getResponseBody()->errors; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if ( $implode ) { |
| 39 | $message = $this->flattenResponse( $message ); |
| 40 | |
| 41 | return implode( ',', (array) $message ); |
| 42 | } |
| 43 | |
| 44 | return $message; |
| 45 | } |
| 46 | |
| 47 | private function flattenResponse( $data = array() ) { |
| 48 | $result = array(); |
| 49 | |
| 50 | if ( ! is_array( $data ) ) { |
| 51 | $data = array( $data ); |
| 52 | } |
| 53 | |
| 54 | foreach ( $data as $values ) { |
| 55 | if ( is_object( $values ) ) { |
| 56 | $values = (array) $values; |
| 57 | } |
| 58 | |
| 59 | if ( is_array( $values ) ) { |
| 60 | $result = array_merge( $result, $this->flattenResponse( $values ) ); |
| 61 | } else { |
| 62 | $result[] = $values; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | public function isSuccess() { |
| 70 | return ( ! $this->isWPError() && $this->getResponseBody() && $this->getResponseBody()->status !== 'error' ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return array|mixed|object |
| 75 | */ |
| 76 | public function getResponseBody() { |
| 77 | return $this->response_body; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return int|string |
| 82 | */ |
| 83 | public function getResponseCode() { |
| 84 | |
| 85 | if ( $this->isWPError() ) { |
| 86 | return 403; |
| 87 | } |
| 88 | |
| 89 | return $this->response_code; |
| 90 | } |
| 91 | |
| 92 | public function isWPError() { |
| 93 | return ( $this->response instanceof \WP_Error ); |
| 94 | } |
| 95 | |
| 96 | public function getWPError() { |
| 97 | return $this->response->get_error_message(); |
| 98 | } |
| 99 | |
| 100 | public function isError() { |
| 101 | return ( $this->isWPError() || ! $this->isSuccess() ); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |