You are here

class RestResponse in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 src/Rest/RestResponse.php \Drupal\salesforce\Rest\RestResponse
  2. 5.0.x src/Rest/RestResponse.php \Drupal\salesforce\Rest\RestResponse

Class RestResponse.

@package Drupal\salesforce\Rest

Hierarchy

  • class \Drupal\salesforce\Rest\RestResponse extends \GuzzleHttp\Psr7\Response

Expanded class hierarchy of RestResponse

2 files declare their use of RestResponse
RestClientTest.php in tests/src/Unit/RestClientTest.php
TestRestClient.php in src/Tests/TestRestClient.php

File

src/Rest/RestResponse.php, line 13

Namespace

Drupal\salesforce\Rest
View source
class RestResponse extends Response {

  /**
   * The original Response used to build this object.
   *
   * @var \GuzzleHttp\Psr7\Response
   * @see __get()
   */
  protected $response;

  /**
   * The json-decoded response body.
   *
   * @var mixed
   * @see __get()
   */
  protected $data;

  /**
   * RestResponse constructor.
   *
   * @param \GuzzleHttp\Psr7\Response $response
   *   A response.
   */
  public function __construct(Response $response) {
    $this->response = $response;
    parent::__construct($response
      ->getStatusCode(), $response
      ->getHeaders(), $response
      ->getBody(), $response
      ->getProtocolVersion(), $response
      ->getReasonPhrase());
    $this
      ->handleJsonResponse();
  }

  /**
   * Magic getter method to return the given property.
   *
   * @param string $key
   *   The property name.
   *
   * @return mixed
   *   The property value.
   *
   * @throws \Exception
   *   If $key property does not exist.
   */
  public function __get($key) {
    if (!property_exists($this, $key)) {
      throw new \Exception("Undefined property {$key}");
    }
    return $this->{$key};
  }

  /**
   * Helper function to eliminate repetitive json parsing.
   *
   * @return $this
   *
   * @throws \Drupal\salesforce\Rest\RestException
   */
  private function handleJsonResponse() {
    $this->data = '';
    $response_body = $this
      ->getBody()
      ->getContents();
    if (empty($response_body)) {
      return NULL;
    }

    // Allow any exceptions here to bubble up:
    try {
      $data = Json::decode($response_body);
    } catch (\Exception $e) {
      throw new RestException($this, $e
        ->getMessage(), $e
        ->getCode(), $e);
    }
    if (empty($data)) {
      throw new RestException($this, t('Invalid response'));
    }
    if (!empty($data['error'])) {
      throw new RestException($this, $data['error']);
    }
    if (!empty($data[0]) && count($data) == 1) {
      $data = $data[0];
    }
    if (!empty($data['error'])) {
      throw new RestException($this, $data['error']);
    }
    if (!empty($data['errorCode'])) {
      throw new RestException($this, $data['errorCode']);
    }
    $this->data = $data;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RestResponse::$data protected property The json-decoded response body.
RestResponse::$response protected property The original Response used to build this object.
RestResponse::handleJsonResponse private function Helper function to eliminate repetitive json parsing.
RestResponse::__construct public function RestResponse constructor. 2
RestResponse::__get public function Magic getter method to return the given property.