You are here

class RestResponse in Salesforce Suite 5.0.x

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

Class RestResponse.

@package Drupal\salesforce\Rest

Hierarchy

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 15

Namespace

Drupal\salesforce\Rest
View source
class RestResponse extends Response {
  use StringTranslationTrait;

  /**
   * 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 \Psr\Http\Message\ResponseInterface $response
   *   A response.
   */
  public function __construct(ResponseInterface $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) || !is_array($data)) {
      throw new RestException($this, $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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.