RestResponse.php in Salesforce Suite 5.0.x
File
src/Rest/RestResponse.php
View source
<?php
namespace Drupal\salesforce\Rest;
use Drupal\Component\Serialization\Json;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
class RestResponse extends Response {
use StringTranslationTrait;
protected $response;
protected $data;
public function __construct(ResponseInterface $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) || !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;
}
}