From a71a9ecad50c675403fb5e98910544870a1248b7 Mon Sep 17 00:00:00 2001 From: Valentin Date: Fri, 16 Aug 2019 17:05:37 -0400 Subject: [PATCH] Fix when filtering a collection, array must be returned --- composer.json | 3 +- src/Provider/Lightspeed.php | 14 +- src/Provider/MerchantOS.php | 283 ++++++++++++++++++++++++++---------- 3 files changed, 217 insertions(+), 83 deletions(-) diff --git a/composer.json b/composer.json index b606612..b35eda5 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "ursuleacv/oauth2-lightspeed", "description": "LightSpeed OAuth 2.0 Client Provider for The PHP League OAuth2-Client", "license": "MIT", - "version": "2.0.27", + "version": "2.0.28", "authors": [ { "name": "Valentin Ursuleac", @@ -22,6 +22,7 @@ "require": { "php": ">=5.5.0", "ext-curl": "*", + "ext-json": "*", "league/oauth2-client": "~1.0", "guzzlehttp/guzzle": "~6.0" }, diff --git a/src/Provider/Lightspeed.php b/src/Provider/Lightspeed.php index d505dfc..cc0f288 100644 --- a/src/Provider/Lightspeed.php +++ b/src/Provider/Lightspeed.php @@ -2,15 +2,13 @@ namespace League\OAuth2\Client\Provider; -use GuzzleHttp\Client; +use InvalidArgumentException; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; -use League\OAuth2\Client\Provider\Exception\LightspeedProviderException; use League\OAuth2\Client\Token\AccessToken; use Psr\Http\Message\ResponseInterface; class Lightspeed extends AbstractProvider { - const LIGHTSPEED_API_URL = 'https://api.merchantos.com/API/'; const LIGHTSPEED_REGISTRATION_ENDPOINT = 'https://cloud.merchantos.com/oauth/register.php'; const LIGHTSPEED_AUTHORIZATION_ENDPOINT = 'https://cloud.merchantos.com/oauth/authorize.php'; @@ -39,6 +37,7 @@ public function getBaseAuthorizationUrl() /** * @param array $params + * @return string */ public function getBaseAccessTokenUrl(array $params) { @@ -53,6 +52,7 @@ public function getDefaultScopes() /** * @param $grant * @param array $params + * @return AccessToken */ public function getAccessToken($grant = 'authorization_code', array $params = []) { @@ -65,8 +65,6 @@ public function getAccessToken($grant = 'authorization_code', array $params = [] * @param string $accessToken * * @return \League\OAuth2\Client\Token\AccessToken - * - * @throws LightspeedProviderException */ public function getLongLivedAccessToken($accessToken) { @@ -79,6 +77,7 @@ public function getLongLivedAccessToken($accessToken) /** * @param AccessToken $token + * @return mixed */ public function getAccountId(AccessToken $token) { @@ -99,6 +98,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token) /** * @param array $response * @param AccessToken $token + * @return LightspeedResourceOwner */ protected function createResourceOwner(array $response, AccessToken $token) { @@ -106,8 +106,9 @@ protected function createResourceOwner(array $response, AccessToken $token) } /** - * @param array $response * @param AccessToken $token + * @param $accountId + * @return MerchantOS */ public function merchantosApi(AccessToken $token, $accountId) { @@ -130,6 +131,7 @@ protected function getRequiredOptions() /** * @param ResponseInterface $response * @param $data + * @throws IdentityProviderException */ protected function checkResponse(ResponseInterface $response, $data) { diff --git a/src/Provider/MerchantOS.php b/src/Provider/MerchantOS.php index 5b7a2aa..2078a77 100644 --- a/src/Provider/MerchantOS.php +++ b/src/Provider/MerchantOS.php @@ -2,6 +2,7 @@ namespace League\OAuth2\Client\Provider; +use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; class MerchantOS extends Lightspeed @@ -98,8 +99,10 @@ public function getConnectTimeout() /** * @param $vendorId - * @param $params + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getVendor($vendorId, $params = []) { @@ -114,77 +117,94 @@ public function getVendor($vendorId, $params = []) } /** - * @param $params + * @param array $params * @return array + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getVendors($params = []) { $response = $this->makeAPICall('Account.Vendor', 'GET', null, $params, null); - if (isset($response['Vendor']) && $this->itemsCount($response) == 1) { - return [$response['Vendor']]; - } elseif (isset($response['Vendor']) && $this->itemsCount($response) > 1) { - return $response['Vendor']; + if (isset($response['Vendor'])) { + if (isset($response['Vendor'][0])) { + return $response['Vendor']; + } else { + return [$response['Vendor']]; + } } return []; } /** - * @param $params + * @param array $params * @return array + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getTags($params = []) { $response = $this->makeAPICall('Account.Tag', 'GET', null, $params, null); - if (isset($response['Tag']) && $this->itemsCount($response) == 1) { - return [$response['Tag']]; - } elseif (isset($response['Tag']) && $this->itemsCount($response) > 1) { - return $response['Tag']; + if (isset($response['Tag'])) { + if (isset($response['Tag'][0])) { + return $response['Tag']; + } else { + return [$response['Tag']]; + } } return []; } /** - * @param $params + * @param array $params * @return array + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCustomerTypes($params = []) { $response = $this->makeAPICall('Account.CustomerType', 'GET', null, $params, null); - if (isset($response['CustomerType']) && $this->itemsCount($response) == 1) { - return [$response['CustomerType']]; - } elseif (isset($response['CustomerType']) && $this->itemsCount($response) > 1) { - return $response['CustomerType']; + if (isset($response['CustomerType'])) { + if (isset($response['CustomerType'][0])) { + return $response['CustomerType']; + } else { + return [$response['CustomerType']]; + } } return []; } /** - * @param $params + * @param array $params * @return array + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCategories($params = []) { $response = $this->makeAPICall('Account.Category', 'GET', null, $params, null); - if (isset($response['Category']) && $this->itemsCount($response) == 1) { - return [$response['Category']]; - } elseif (isset($response['Category']) && $this->itemsCount($response) > 1) { - return $response['Category']; + if (isset($response['Category'])) { + if (isset($response['Category'][0])) { + return $response['Category']; + } else { + return [$response['Category']]; + } } - return []; } /** * @param $itemId - * @param $params + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getItem($itemId, $params = []) { @@ -198,26 +218,31 @@ public function getItem($itemId, $params = []) } /** - * @param $params + * @param array $params * @return array + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getItems($params = []) { $response = $this->makeAPICall('Account.Item', 'GET', null, $params, null); - if (isset($response['Item']) && $this->itemsCount($response) == 1) { - return [$response['Item']]; - } elseif (isset($response['Item']) && $this->itemsCount($response) > 1) { - return $response['Item']; + if (isset($response['Item'])) { + if (isset($response['Item'][0])) { + return $response['Item']; + } else { + return [$response['Item']]; + } } return []; } /** - * @param $itemId * @param $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createItem($data) { @@ -234,6 +259,8 @@ public function createItem($data) /** * @param $itemId * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function deleteItem($itemId) { @@ -251,6 +278,8 @@ public function deleteItem($itemId) * @param $itemId * @param $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateItem($itemId, $data) { @@ -268,6 +297,8 @@ public function updateItem($itemId, $data) * @param int $saleId * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getSale($saleId, $params = []) { @@ -292,6 +323,8 @@ public function getSale($saleId, $params = []) * @param int $saleId * @param array $saleData * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateSale($saleId, $saleData) { @@ -308,15 +341,19 @@ public function updateSale($saleId, $saleData) /** * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getSales($params = []) { $response = $this->makeAPICall('Account.Sale', 'GET', null, $params, null); - if (isset($response['Sale']) && $this->itemsCount($response) == 1) { - return [$response['Sale']]; - } elseif (isset($response['Sale']) && $this->itemsCount($response) > 1) { - return $response['Sale']; + if (isset($response['Sale'])) { + if (isset($response['Sale'][0])) { + return $response['Sale']; + } else { + return [$response['Sale']]; + } } return []; @@ -326,15 +363,19 @@ public function getSales($params = []) * @param int $saleId * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getSaleSaleLines($saleId, $params = []) { $response = $this->makeAPICall('Account.Sale' . '/' . $saleId . '/SaleLine', 'GET', null, $params, null); - if (isset($response['SaleLine']) && $this->itemsCount($response) == 1) { - return [$response['SaleLine']]; - } elseif (isset($response['SaleLine']) && $this->itemsCount($response) > 1) { - return $response['SaleLine']; + if (isset($response['SaleLine'])) { + if (isset($response['SaleLine'][0])) { + return $response['SaleLine']; + } else { + return [$response['SaleLine']]; + } } return []; @@ -343,24 +384,28 @@ public function getSaleSaleLines($saleId, $params = []) /** * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getSaleLines($params = []) { $response = $this->makeAPICall('Account.SaleLine', 'GET', null, $params, null); - if (isset($response['SaleLine']) && $this->itemsCount($response) == 1) { - return [$response['SaleLine']]; - } elseif (isset($response['SaleLine']) && $this->itemsCount($response) > 1) { - return $response['SaleLine']; + if (isset($response['SaleLine'])) { + if (isset($response['SaleLine'][0])) { + return $response['SaleLine']; + } else { + return [$response['SaleLine']]; + } } - return []; } /** - * @param int $saleId - * @param int $limit + * @param $saleLineId * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getSaleLine($saleLineId) { @@ -379,6 +424,8 @@ public function getSaleLine($saleLineId) * @param $saleLineId * @param $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateSaleLine($saleId, $saleLineId, $data) { @@ -398,6 +445,8 @@ public function updateSaleLine($saleId, $saleLineId, $data) * @param $saleId * @param $saleLineId * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function deleteSaleLine($saleId, $saleLineId) { @@ -412,10 +461,13 @@ public function deleteSaleLine($saleId, $saleLineId) return []; } + /** * @param $saleId * @param $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createSaleLine($saleId, $data) { @@ -431,18 +483,22 @@ public function createSaleLine($saleId, $data) } /** - * @param $params + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getShops($params = []) { $response = $this->makeAPICall('Account.Shop', 'GET', null, $params, null); //validate the response - if (isset($response['Shop']) && $this->itemsCount($response) == 1) { - return [$response['Shop']]; - } elseif (isset($response['Shop']) && $this->itemsCount($response) > 1) { - return $response['Shop']; + if (isset($response['Shop'])) { + if (isset($response['Shop'][0])) { + return $response['Shop']; + } else { + return [$response['Shop']]; + } } return []; @@ -450,8 +506,10 @@ public function getShops($params = []) /** * @param $shopId - * @param $params + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getShop($shopId, $params = []) { @@ -467,7 +525,10 @@ public function getShop($shopId, $params = []) /** * @param int $customerId + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCustomer($customerId, $params = []) { @@ -493,16 +554,20 @@ public function getCustomer($customerId, $params = []) /** * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCustomers($params) { $response = $this->makeAPICall('Account.Customer', 'GET', null, $params, null); //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']; + if (isset($response['Customer'])) { + if (isset($response['Customer'][0])) { + return $response['Customer']; + } else { + return [$response['Customer']]; + } } return []; @@ -511,6 +576,8 @@ public function getCustomers($params) /** * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createCustomer($data) { @@ -530,6 +597,8 @@ public function createCustomer($data) * @param int $customerId * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateCustomer($customerId, $data) { @@ -549,23 +618,31 @@ public function updateCustomer($customerId, $data) /** * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCustomFields($params) { $response = $this->makeAPICall('Account.Customer/CustomField', 'GET', null, $params, null); //validate the response - if (isset($response['CustomField']) && $this->itemsCount($response) == 1) { - return [$response['CustomField']]; - } elseif (isset($response['CustomField']) && $this->itemsCount($response) > 1) { - return $response['CustomField']; + if (isset($response['CustomField'])) { + if (isset($response['CustomField'][0])) { + return $response['CustomField']; + } else { + return [$response['CustomField']]; + } } return []; } /** + * @param $customFieldId + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCustomField($customFieldId, $params = []) { @@ -592,6 +669,8 @@ public function getCustomField($customFieldId, $params = []) /** * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createCustomField($data) { @@ -612,6 +691,8 @@ public function createCustomField($data) * @param integer $customFieldId * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getAllCustomFieldChoices($customFieldId, $params = []) { @@ -619,10 +700,12 @@ public function getAllCustomFieldChoices($customFieldId, $params = []) . $customFieldId . '/CustomFieldChoice', 'GET', null, $params, null); //validate the response - if (isset($response['CustomFieldChoice']) && $this->itemsCount($response) == 1) { - return [$response['CustomFieldChoice']]; - } elseif (isset($response['CustomFieldChoice']) && $this->itemsCount($response) > 1) { - return $response['CustomFieldChoice']; + if (isset($response['CustomFieldChoice'])) { + if (isset($response['CustomFieldChoice'][0])) { + return $response['CustomFieldChoice']; + } else { + return [$response['CustomFieldChoice']]; + } } return []; @@ -633,6 +716,8 @@ public function getAllCustomFieldChoices($customFieldId, $params = []) * @param integer $customFieldChoiceID * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCustomFieldChoice($customFieldId, $customFieldChoiceID, $params = []) { @@ -653,6 +738,8 @@ public function getCustomFieldChoice($customFieldId, $customFieldChoiceID, $para * @param integer $customFieldId * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createCustomFieldChoice($customFieldId, $data) { @@ -673,6 +760,8 @@ public function createCustomFieldChoice($customFieldId, $data) /** * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createTaxClass($data) { @@ -693,6 +782,8 @@ public function createTaxClass($data) * @param int $taxClassId * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getTaxClass($taxClassId, $params) { @@ -709,8 +800,10 @@ public function getTaxClass($taxClassId, $params) } /** - * @param array $data + * @param $customFieldId * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function deleteCustomField($customFieldId) { @@ -727,7 +820,10 @@ public function deleteCustomField($customFieldId) /** * @param int $employeeId + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getEmployee($employeeId, $params = []) { @@ -753,41 +849,50 @@ public function getEmployee($employeeId, $params = []) /** * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getEmployees($params = []) { $response = $this->makeAPICall('Account.Employee', 'GET', null, $params, null); //validate the response - if (isset($response['Employee']) && $this->itemsCount($response) == 1) { - return [$response['Employee']]; - } elseif (isset($response['Employee']) && $this->itemsCount($response) > 0) { - return $response['Employee']; + if (isset($response['Employee'])) { + if (isset($response['Employee'][0])) { + return $response['Employee']; + } else { + return [$response['Employee']]; + } } return []; } /** - * @param $params + * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getDiscounts($params = []) { $response = $this->makeAPICall('Account.Discount', 'GET', null, $params, null); - if (isset($response['Discount']) && $this->itemsCount($response) == 1) { - return [$response['Discount']]; - } elseif (isset($response['Discount']) && $this->itemsCount($response) > 0) { - return $response['Discount']; + if (isset($response['Discount'])) { + if (isset($response['Discount'][0])) { + return $response['Discount']; + } else { + return [$response['Discount']]; + } } - return []; } /** * @param $discountId * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getDiscount($discountId = null) { @@ -804,6 +909,8 @@ public function getDiscount($discountId = null) /** * @param $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createDiscount($data) { @@ -822,6 +929,8 @@ public function createDiscount($data) * @param $discountId * @param $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateDiscount($discountId, $data) { @@ -838,6 +947,8 @@ public function updateDiscount($discountId, $data) /** * @param $discountId * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function deleteDiscount($discountId = null) { @@ -855,6 +966,8 @@ public function deleteDiscount($discountId = null) * @param int $saleId * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createSaleRefund($saleId, $data = []) { @@ -870,6 +983,8 @@ public function createSaleRefund($saleId, $data = []) /** * @param array $params * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getCreditAccount($params = []) { @@ -886,6 +1001,8 @@ public function getCreditAccount($params = []) * @param int $creditAccountId * @param array $data * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function createCreditAccount($creditAccountId, $data = []) { @@ -900,6 +1017,8 @@ public function createCreditAccount($creditAccountId, $data = []) /** * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function accounts() { @@ -914,6 +1033,8 @@ public function accounts() /** * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getConfig() { @@ -928,6 +1049,8 @@ public function getConfig() /** * @return mixed + * @throws IdentityProviderException + * @throws \GuzzleHttp\Exception\GuzzleException */ public function getOptions() { @@ -938,6 +1061,8 @@ public function getOptions() /** * @return mixed + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws IdentityProviderException */ public function getLocale() { @@ -957,6 +1082,8 @@ public function getLocale() * @param $params * @param $data * @return mixed + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws IdentityProviderException */ public function makeAPICall($controlUrl, $action, $uniqueId, $params, $data) { @@ -1056,6 +1183,7 @@ protected function buildQueryString(array $data) /** * @param $response + * @throws IdentityProviderException */ private function checkApiResponse($response) { @@ -1082,8 +1210,7 @@ private function itemsCount($response) } /** - * @param $headers - * @return null + * @return void */ protected function sleepIfNecessary() { @@ -1111,7 +1238,7 @@ protected function sleepIfNecessary() //remaining units until the limit is reached $available = $bucketSize - $currentLevel; - //get the units neded for the next request + //get the units needed for the next request $units = $this->getMethodUnits(); if ($units >= $available) { @@ -1134,6 +1261,9 @@ protected function sleepIfNecessary() } } + /** + * @return int + */ private function getMethodUnits() { if ($this->context['action'] == 'GET') { @@ -1141,5 +1271,6 @@ private function getMethodUnits() } elseif (in_array($this->context['action'], ['POST', 'PUT', 'DELETE'])) { return 10; } + return 1; } }