Skip to content

Commit

Permalink
Merge pull request #4 from ursuleacv/1.0-dev
Browse files Browse the repository at this point in the history
Adding resource owner
  • Loading branch information
ursuleacv committed Mar 17, 2016
2 parents b39f099 + f6263b1 commit 1e9b2bd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ursuleacv/oauth2-lightspeed",
"description": "LightSpeed OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"version": "1.0.0",
"version": "1.0.2",
"authors": [
{
"name": "Valentin Ursuleac",
Expand Down
77 changes: 45 additions & 32 deletions src/Provider/Lightspeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class Lightspeed extends AbstractProvider
const LIGHTSPEED_TOKEN_ENDPOINT = 'https://cloud.merchantos.com/oauth/access_token.php';
const LS_FORMAT = '.json';

/**
* @var mixed
*/
protected $accountId;

/**
* @var array
*/
Expand All @@ -35,8 +30,6 @@ class Lightspeed extends AbstractProvider
public function __construct($options = [], array $collaborators = [])
{
parent::__construct($options, $collaborators);

$this->accountId = $options['accountId'];
}

public function getBaseAuthorizationUrl()
Expand Down Expand Up @@ -93,20 +86,9 @@ public function getLongLivedAccessToken($accessToken)
*/
public function getAccountId(AccessToken $token)
{
$params = ['oauth_token' => $token->getToken()];
$url = $this->prepareApiUrl('Account', $this->accountId, null, $params);
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);

$response = $this->getResponse($request);

if (isset($response['Account']) && $response['Account']['accountID']) {
return (int) $response['Account']['accountID'];
}

if (isset($response['httpCode']) && $response['httpCode'] != '200') {
throw new IdentityProviderException($response['message'], $response['httpCode'], $response);
}
$account = $this->getResourceOwner($token);

return $account->getId();
}

/**
Expand All @@ -119,8 +101,10 @@ public function getSale(AccessToken $token, $saleId)
$apiResource = 'Account.Sale';
$this->context['apiCall'] = $apiResource;

$account = $this->getResourceOwner($token);

$params = ['oauth_token' => $token->getToken()];
$url = $this->prepareApiUrl($apiResource, $this->accountId, $saleId, $params);
$url = $this->prepareApiUrl($apiResource, $account->getId(), $saleId, $params);
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
$response = $this->getResponse($request);

Expand All @@ -143,9 +127,11 @@ public function getShops(AccessToken $token)
$this->context['apiCall'] = $apiResource;

$params = ['oauth_token' => $token->getToken()];

$account = $this->getResourceOwner($token);

//get url
$url = $this->prepareApiUrl($apiResource, $this->accountId, null, $params);
$url = $this->prepareApiUrl($apiResource, $account->getId(), null, $params);
//make API call
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
//get response
Expand All @@ -171,7 +157,7 @@ public function getCustomer(AccessToken $token, $customerId)
{
$apiResource = 'Account.Customer';
$this->context['apiCall'] = $apiResource;

$account = $this->getResourceOwner($token);
$params = array(
'oauth_token' => $token->getToken(),
'archived' => 0,
Expand All @@ -181,7 +167,7 @@ public function getCustomer(AccessToken $token, $customerId)
);

//get url
$url = $this->prepareApiUrl($apiResource, $this->accountId, null, $params);
$url = $this->prepareApiUrl($apiResource, $account->getId(), null, $params);
//make API call
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
//get response
Expand All @@ -199,6 +185,37 @@ public function getCustomer(AccessToken $token, $customerId)
return [];
}

/**
* @param AccessToken $token
* @param integer $employeeId
* @return mixed
*/
public function getEmployee(AccessToken $token, $employeeId)
{
$apiResource = 'Account.Employee';
$this->context['apiCall'] = $apiResource;
$account = $this->getResourceOwner($token);
$params = array(
'oauth_token' => $token->getToken(),
'archived' => 0,
'limit' => '1',
'load_relations' => 'all',
'employeeID' => $employeeId,
);

$url = $this->prepareApiUrl($apiResource, $account->getId(), null, $params); //get url
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token); //make API call
$response = $this->getResponse($request); //get response
$this->checkApiResponse($response); //check if there is an error

//validate the response
if (isset($response['Employee']) && $this->itemsCount($response) > 0) {
return $response['Employee'];
}

return [];
}

/**
* @param $controlName
* @param $accountId
Expand All @@ -208,7 +225,8 @@ public function getCustomer(AccessToken $token, $customerId)
*/
private function prepareApiUrl($controlName, $accountId, $uniqueId = null, $queryStr = null)
{
$controlUrl = $this->getBaseLightspeedApiUrl() . str_replace('.', '/', str_replace('Account.', 'Account.' . $accountId . '.', $controlName));
$controlUrl = $this->getBaseLightspeedApiUrl();
$controlUrl .= str_replace('.', '/', str_replace('Account.', 'Account.' . $accountId . '.', $controlName));

if ($uniqueId) {
$controlUrl .= '/' . $uniqueId;
Expand Down Expand Up @@ -247,11 +265,6 @@ private function buildQueryString($data)
*/
private function checkApiResponse($response)
{
if (empty($this->accountId)) {
$message = 'The "accountId" not set. In order to query Shop endpoint an accountId is required.';
throw new \Exception($message);
}

// must be an error
if (isset($response['httpCode']) && $response['httpCode'] != '200') {
$message = $response['httpMessage'] . ': ' . $response['message'] . ' (' . $response['errorClass'] . ')';
Expand Down Expand Up @@ -280,7 +293,7 @@ private function itemsCount($response)
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return $this->getBaseLightspeedApiUrl() . '/Account/' . $this->accountId . '/Item?oauth_token=' . $token;
return $this->getBaseLightspeedApiUrl() . 'Account/.json?oauth_token=' . $token;
}

/**
Expand All @@ -289,7 +302,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token)
*/
protected function createResourceOwner(array $response, AccessToken $token)
{
return new LightspeedUser($response);
return new LightspeedResourceOwner($response);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions src/Provider/LightspeedResourceOwner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace League\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

class LightspeedResourceOwner implements ResourceOwnerInterface
{
/**
* Raw response
*
* @var array
*/
protected $response;

/**
* Creates new resource owner.
*
* @param array $response
*/
public function __construct(array $response = array())
{
$this->response = $response;
}

/**
* Get Account ID
*
* @return string|null
*/
public function getId()
{
return $this->getResponseData('Account.accountID');
}

/**
* Attempts to pull value from array using dot notation.
*
* @param string $path
* @param string $default
*
* @return mixed
*/
protected function getResponseData($path, $default = null)
{
$array = $this->response;
if (!empty($path)) {
$keys = explode('.', $path);
foreach ($keys as $key) {
if (isset($array[$key])) {
$array = $array[$key];
} else {
return $default;
}
}
}
return $array;
}

/**
* Return all of the owner details available as an array.
*
* @return array
*/
public function toArray()
{
return $this->response;
}
}

0 comments on commit 1e9b2bd

Please sign in to comment.