HEX
Server: LiteSpeed
System: Linux server315.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: globfdxw (6114)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: /home/globfdxw/www/wp-content/plugins/wpforms-google-calendar/src/Api/Http/Response.php
<?php

namespace WPFormsGoogleCalendar\Api\Http;

// phpcs:ignore WPForms.PHP.UseStatement.UnusedUseStatement
use WP_Error;

/**
 * Wrapper class to parse responses.
 *
 * @since 1.0.0
 */
class Response {

	/**
	 * Input data.
	 *
	 * @since 1.0.0
	 *
	 * @var array|WP_Error
	 */
	private $input;

	/**
	 * Response constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param array|WP_Error $input The response data.
	 */
	public function __construct( $input ) {

		$this->input = $input;
	}

	/**
	 * Retrieve only the response code from the raw response.
	 *
	 * @since 1.0.0
	 *
	 * @return int The response code as an integer.
	 */
	private function get_response_code(): int {

		return absint( wp_remote_retrieve_response_code( $this->input ) );
	}

	/**
	 * Retrieve only the body from the raw response.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_body(): array {

		if ( $this->has_errors() ) {
			return [];
		}

		return (array) json_decode( wp_remote_retrieve_body( $this->input ), true );
	}

	/**
	 * Retrieves the raw body of the response.
	 *
	 * @since 1.0.0
	 *
	 * @return string The raw response body, or an empty string if there are errors.
	 */
	public function get_file(): string {

		if ( $this->has_errors() ) {
			return '';
		}

		return wp_remote_retrieve_body( $this->input );
	}

	/**
	 * Retrieve only the location from the raw response.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_location(): string {

		if ( $this->has_errors() ) {
			return '';
		}

		return (string) wp_remote_retrieve_header( $this->input, 'Location' );
	}

	/**
	 * Whether we received errors in the response.
	 *
	 * @since 1.0.0
	 *
	 * @return bool True if the response has errors.
	 */
	public function has_errors(): bool {

		$code = $this->get_response_code();

		return $code < 200 || $code > 299;
	}
}