Skip to content

Commit

Permalink
Merge pull request #3 from ursuleacv/1.0-dev
Browse files Browse the repository at this point in the history
1.0.0
  • Loading branch information
ursuleacv committed Mar 16, 2016
2 parents 8d16904 + 1103687 commit b39f099
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ before_script:
- travis_retry phpenv rehash

script:
- ./vendor/bin/phpcs --standard=psr2 src/
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- ./vendor/bin/phpcs --standard=psr2 --warning-severity=0 src/
- ./vendor/bin/phpunit --coverage-text

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "7.0" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# LightSpeed Provider for OAuth 2.0 Client

[![Build Status](https://travis-ci.org/ursuleacv/oauth2-lightspeed.png?branch=master)](https://travis-ci.org/ursuleacv/oauth2-lightspeed)

This package provides LightSpeed OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/ursuleacv/oauth2-client).

This package is compliant with [PSR-1][], [PSR-2][], [PSR-4][], and [PSR-7][]. If you notice compliance oversights, please send a patch via pull request.
Expand Down
76 changes: 70 additions & 6 deletions src/Provider/Lightspeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function getLongLivedAccessToken($accessToken)
*/
public function getAccountId(AccessToken $token)
{
$url = $this->prepareApiUrl('Account', $this->accountId, null) . '?oauth_token=' . $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);
Expand All @@ -118,7 +119,8 @@ public function getSale(AccessToken $token, $saleId)
$apiResource = 'Account.Sale';
$this->context['apiCall'] = $apiResource;

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

Expand All @@ -140,8 +142,10 @@ public function getShops(AccessToken $token)
$apiResource = 'Account.Shop';
$this->context['apiCall'] = $apiResource;

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

//get url
$url = $this->prepareApiUrl($apiResource, $this->accountId, null) . '?oauth_token=' . $token;
$url = $this->prepareApiUrl($apiResource, $this->accountId, null, $params);
//make API call
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
//get response
Expand All @@ -159,25 +163,85 @@ public function getShops(AccessToken $token)
return [];
}

/**
* @param AccessToken $token
* @return mixed
*/
public function getCustomer(AccessToken $token, $customerId)
{
$apiResource = 'Account.Customer';
$this->context['apiCall'] = $apiResource;

$params = array(
'oauth_token' => $token->getToken(),
'archived' => 0,
'limit' => '50',
'load_relations' => 'all',
'customerID' => $customerId,
);

//get url
$url = $this->prepareApiUrl($apiResource, $this->accountId, null, $params);
//make API call
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token);
//get response
$response = $this->getResponse($request);

$this->checkApiResponse($response);

//validate the response
if (isset($response['Customer']) && $this->itemsCount($response) == 1) {
return $response['Customer'];
} elseif (isset($response['Customer']) && $this->itemsCount($response) > 1) {
return $response['Customer'];
}

return [];
}

/**
* @param $controlName
* @param $accountId
* @param $uniqueId
* @return mixed
* @param $queryStr
* @return string
*/
private function prepareApiUrl($controlName, $accountId, $uniqueId = null)
private function prepareApiUrl($controlName, $accountId, $uniqueId = null, $queryStr = null)
{
$controlUrl = $this->getBaseLightspeedApiUrl() . str_replace('.', '/', str_replace('Account.', 'Account.' . $accountId . '.', $controlName));

if ($uniqueId) {
$controlUrl .= '/' . $uniqueId;
}
if ($queryStr && is_array($queryStr)) {
$_queryStr = $this->buildQueryString($queryStr);

$controlUrl .= self::LS_FORMAT;
$controlUrl .= self::LS_FORMAT . '?' . $_queryStr;
} else {
$controlUrl .= self::LS_FORMAT;
}

return $controlUrl;
}

/**
* @param array $data
* @return string
*/
private function buildQueryString($data)
{
if (function_exists('http_build_query')) {
return http_build_query($data);
} else {
$qs = '';
foreach ($data as $key => $value) {
$append = urlencode($key) . '=' . urlencode($value);
$qs .= $qs ? '&' . $append : $append;
}
return $qs;
}
}

/**
* @param $response
*/
Expand Down
40 changes: 0 additions & 40 deletions tests/src/Provider/LightspeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,4 @@ public function testScopes()
// $this->assertEquals(12345, $user->getAccountId($token));
// }

/**
* @expectedException \InvalidArgumentException
*/
// public function testNotSettingADefaultAccountIdWillThrow()
// {
// new Lightspeed([
// 'clientId' => 'mock_client_id',
// 'clientSecret' => 'mock_secret',
// 'redirectUri' => 'none',
// 'accountId' => ,
// ]);
// }

public function testProperlyHandlesErrorResponses()
{
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getHeader')
->times(1)
->andReturn('application/json');
$postResponse->shouldReceive('getBody')
->times(1)
->andReturn('{"error":{"message":"Foo auth error","type":"OAuthException","code":191}}');

$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($postResponse);
$this->provider->setHttpClient($client);

$errorMessage = '';
$errorCode = 0;

try {
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
} catch (IdentityProviderException $e) {
$errorMessage = $e->getMessage();
$errorCode = $e->getCode();
}

$this->assertEquals('OAuthException: Foo auth error', $errorMessage);
$this->assertEquals(191, $errorCode);
}
}

0 comments on commit b39f099

Please sign in to comment.