RestResponse.php in Salesforce Suite 8.3
File
src/Rest/RestResponse.php
View source
<?php
namespace Drupal\salesforce\Rest;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\Psr7\Response;
class RestResponse extends Response {
protected $response;
protected $data;
public function __construct(Response $response) {
$this->response = $response;
parent::__construct($response
->getStatusCode(), $response
->getHeaders(), $response
->getBody(), $response
->getProtocolVersion(), $response
->getReasonPhrase());
$this
->handleJsonResponse();
}
public function __get($key) {
if (!property_exists($this, $key)) {
throw new \Exception("Undefined property {$key}");
}
return $this->{$key};
}
private function handleJsonResponse() {
$this->data = '';
$response_body = $this
->getBody()
->getContents();
if (empty($response_body)) {
return NULL;
}
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;
}
}