Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| BaseHiiveController | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
| 3 | |
| 4 | /** |
| 5 | * Base class for Hiive related actions. |
| 6 | */ |
| 7 | abstract class BaseHiiveController extends \WP_REST_Controller { |
| 8 | |
| 9 | /** |
| 10 | * Hiive Base URL |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $url; |
| 15 | |
| 16 | /** |
| 17 | * BaseHiiveController constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | |
| 21 | if ( ! defined( 'NFD_HIIVE_BASE_URL' ) ) { |
| 22 | define( 'NFD_HIIVE_BASE_URL', 'https://hiive.cloud' ); |
| 23 | } |
| 24 | |
| 25 | $this->url = NFD_HIIVE_BASE_URL; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get data from the endpoint containing the specific Hiive response. |
| 30 | * |
| 31 | * @param string $endpoint Endpoint request to get specific response |
| 32 | * @param array $args Arguments determining response |
| 33 | * @return \WP_Error|string |
| 34 | */ |
| 35 | protected function get( $endpoint, $args = array() ) { |
| 36 | $request = $this->url . $endpoint . '?' . http_build_query( $args ); |
| 37 | |
| 38 | $response = \wp_remote_get( $request ); |
| 39 | if ( 200 === \wp_remote_retrieve_response_code( $response ) ) { |
| 40 | $payload = \wp_remote_retrieve_body( $response ); |
| 41 | |
| 42 | return $payload; |
| 43 | } |
| 44 | |
| 45 | return new \WP_Error( |
| 46 | 'hiive-error', |
| 47 | 'Error in fetching data.', |
| 48 | array( 'status' => \wp_remote_retrieve_response_code( $response ) ) |
| 49 | ); |
| 50 | } |
| 51 | } |