commerceguys_marketplace.base.inc in Commerce Guys Marketplace 7
File
includes/commerceguys_marketplace.base.inc
View source
<?php
class CommerceGuysMarketplaceNoCredentialsException extends RuntimeException {
}
class CommerceGuysMarketplaceAuthorizationException extends RuntimeException {
}
abstract class CommerceGuysMarketplaceManagerBase {
protected $loadedItems;
function __construct($client, $endpoint, $resource = NULL) {
if (empty($resource)) {
throw new Exception("Cannot create an instance of marketplace manager without a specified resource.");
}
$this->client = $client;
$this->endpoint = $endpoint;
$this->resource = $resource;
}
public function load($id) {
if (!isset($this->loadedItems[$id])) {
$item = FALSE;
$response = drupal_http_request($this->endpoint . '/' . $this->resource . '/' . $id);
$data = json_decode($response->data);
if (is_object($data)) {
$item = $data;
}
$this->loadedItems[$id] = $item;
}
return $this->loadedItems[$id];
}
protected function getAccessToken() {
if (empty($this->client)) {
throw new CommerceGuysMarketplaceNoCredentialsException("Missing client credentials.");
}
$access_token = variable_get('commerceguys_marketplace_access_token', array());
if (empty($access_token['token']) || $access_token['expires'] < time()) {
$data = array(
'grant_type' => 'client_credentials',
'client_id' => $this->client['client_key'],
'client_secret' => $this->client['client_secret'],
);
$options = array(
'method' => 'POST',
'data' => http_build_query($data),
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
);
$response = drupal_http_request(COMMERCEGUYS_MARKETPLACE_URL . '/oauth2/token', $options);
$result = json_decode($response->data);
if ($response->code != 200 || isset($result->error)) {
throw new CommerceGuysMarketplaceAuthorizationException($result->error_description);
}
$access_token = array(
'token' => $result->access_token,
'expires' => time() + $result->expires_in,
);
variable_set('commerceguys_marketplace_access_token', $access_token);
}
return $access_token;
}
}