Example #1
0
 /**
  * Sets properties based on response body.
  *
  * @throws ResponseException
  */
 private function processRawBody()
 {
     switch ($this->contentType) {
         case self::CONTENT_TYPE_JSON:
             try {
                 $responseJson = ContentApiSdk::getValidJsonObj($this->rawBody);
             } catch (InvalidDataException $e) {
                 throw new ResponseException($e->getMessage(), $e->getCode(), $e);
             }
             $this->type = $responseJson->_links->self->title;
             $this->href = $responseJson->_links->self->href;
             if (property_exists($responseJson, '_meta')) {
                 $this->page = $responseJson->_meta->page;
                 $this->maxResults = $responseJson->_meta->max_results;
                 $this->total = $responseJson->_meta->total;
                 $this->nextPage = property_exists($responseJson->_links, 'next') ? $this->page + 1 : $this->page;
                 $this->prevPage = property_exists($responseJson->_links, 'prev') ? $this->page - 1 : $this->page;
                 $this->lastPage = property_exists($responseJson->_links, 'last') ? (int) ceil($this->total / $this->maxResults) : $this->page;
                 $this->resources = $responseJson->_items;
             } else {
                 $resourceObj = new stdClass();
                 foreach ($responseJson as $key => $value) {
                     if (in_array($key, $this->metaKeys)) {
                         continue;
                     }
                     $resourceObj->{$key} = $value;
                 }
                 $this->resources = $resourceObj;
             }
             break;
         case self::CONTENT_TYPE_XML:
             break;
     }
     return;
 }
 /**
  * {@inheritdoc}
  */
 public function getAuthenticationTokens()
 {
     try {
         $response = $this->client->makeCall($this->getAuthenticationUrl(), array(), array(), 'POST', array('client_id' => $this->getClientId(), 'grant_type' => self::AUTHENTICATION_GRANT_TYPE, 'username' => $this->getUsername(), 'password' => $this->getPassword()));
     } catch (ClientException $e) {
         throw new AuthenticationException('Could not request access token.', $e->getCode(), $e);
     }
     if ($response['status'] === 200) {
         try {
             $responseObj = ContentApiSdk::getValidJsonObj($response['body']);
         } catch (InvalidDataException $e) {
             throw new AuthenticationException('Authentication response body is not (valid) json.', $e->getCode(), $e);
         }
         if (property_exists($responseObj, 'access_token')) {
             $this->accessToken = $responseObj->access_token;
             return true;
         }
         throw new AuthenticationException('The server returned an unexpected response body.');
     }
     throw new AuthenticationException(sprintf('The server returned an error with status %s.', $response['status']), $response['status']);
 }