diff --git a/src/Actions/StoreTokenAction.php b/src/Actions/StoreTokenAction.php new file mode 100644 index 0000000..977b9eb --- /dev/null +++ b/src/Actions/StoreTokenAction.php @@ -0,0 +1,32 @@ + $token['id_token'], + 'access_token' => config('xero.encrypt') ? Crypt::encryptString($token['access_token']) : $token['access_token'], + 'expires_in' => $token['expires_in'], + 'token_type' => $token['token_type'], + 'refresh_token' => config('xero.encrypt') ? Crypt::encryptString($token['refresh_token']) : $token['refresh_token'], + 'scopes' => $token['scope'], + ]; + + if ($tenantId) { + $where = ['tenant_id' => $tenantId]; + } elseif ($tenantData !== []) { + $data = array_merge($data, $tenantData); + $where = ['tenant_id' => $data['tenant_id']]; + } else { + $where = ['id' => 1]; + } + + return XeroToken::updateOrCreate($where, $data); + } +} \ No newline at end of file diff --git a/src/Actions/formatQueryStringsAction.php b/src/Actions/formatQueryStringsAction.php new file mode 100644 index 0000000..244edcc --- /dev/null +++ b/src/Actions/formatQueryStringsAction.php @@ -0,0 +1,17 @@ + $value) { + $queryString .= "$key=$value&"; + } + + return rtrim($queryString, '&'); + } +} \ No newline at end of file diff --git a/src/Xero.php b/src/Xero.php index 08b661b..ad64b78 100755 --- a/src/Xero.php +++ b/src/Xero.php @@ -2,6 +2,8 @@ namespace Dcblogdev\Xero; +use Dcblogdev\Xero\Actions\formatQueryStringsAction; +use Dcblogdev\Xero\Actions\StoreTokenAction; use Dcblogdev\Xero\Actions\tokenExpiredAction; use Dcblogdev\Xero\Models\XeroToken; use Dcblogdev\Xero\Resources\Contacts; @@ -128,7 +130,7 @@ public function connect() 'updated_date_utc' => $tenant['updatedDateUtc'], ]; - $this->storeToken($result, $tenantData); + app(StoreTokenAction::class)($result, $tenantData, $tenant['tenantId']); } } catch (Exception $e) { throw new Exception('Error getting tenant: '.$e->getMessage()); @@ -211,8 +213,7 @@ public function renewExpiringToken(XeroToken $token): string $result = $this->sendPost(self::$tokenUrl, $params); app(tokenExpiredAction::class)($result, $token); - - $this->storeToken($result, ['tenant_id' => $token->tenant_id]); + app(StoreTokenAction::class)($result, ['tenant_id' => $token->tenant_id], $this->tenant_id); return $result['access_token']; } @@ -273,32 +274,6 @@ protected function redirectIfNoToken(string $token, bool $redirectWhenNotConnect return false; } - /** - * Store token - */ - protected function storeToken(array $token, array $tenantData = []): object - { - $data = [ - 'id_token' => $token['id_token'], - 'access_token' => config('xero.encrypt') ? Crypt::encryptString($token['access_token']) : $token['access_token'], - 'expires_in' => $token['expires_in'], - 'token_type' => $token['token_type'], - 'refresh_token' => config('xero.encrypt') ? Crypt::encryptString($token['refresh_token']) : $token['refresh_token'], - 'scopes' => $token['scope'], - ]; - - if ($this->tenant_id) { - $where = ['tenant_id' => $this->tenant_id]; - } elseif ($tenantData !== []) { - $data = array_merge($data, $tenantData); - $where = ['tenant_id' => $data['tenant_id']]; - } else { - $where = ['id' => 1]; - } - - return XeroToken::updateOrCreate($where, $data); - } - /** * run guzzle to process requested url * @@ -351,12 +326,6 @@ protected static function sendPost(string $url, array $params) public function formatQueryStrings(array $params): string { - $queryString = ''; - - foreach ($params as $key => $value) { - $queryString .= "$key=$value&"; - } - - return rtrim($queryString, '&'); + return app(formatQueryStringsAction::class)($params); } } diff --git a/tests/Actions/formatQueryStringsActionTest.php b/tests/Actions/formatQueryStringsActionTest.php new file mode 100644 index 0000000..4c501ec --- /dev/null +++ b/tests/Actions/formatQueryStringsActionTest.php @@ -0,0 +1,31 @@ + 'value1', + 'key2' => 'value2', + 'key3' => 'value3', + ]; + + $response = app(formatQueryStringsAction::class)($params); + + expect($response)->toBe('key1=value1&key2=value2&key3=value3'); + +}); + +test('throws type error when non-array is passed', function ($value) { + $this->expectException(TypeError::class); + + app(formatQueryStringsAction::class)($value); +})->with([ + 'string', + 123, + 1.23, + true, + false, + null, + new stdClass(), +]); \ No newline at end of file