Skip to content

Commit

Permalink
Update type of tenant_id and refactor methods in Xero.php
Browse files Browse the repository at this point in the history
The tenant_id variable type has been updated from mixed to string. The 'connect' function and 'getTokenData' return type have been refined to improve readability and avoid potential bugs. Unnecessary comments and code blocks have been also cleaned up for better code maintainability.
  • Loading branch information
dcblogdev committed Jun 19, 2024
1 parent 081acbb commit 393d644
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Xero.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Xero
protected static string $connectionUrl = 'https://api.xero.com/connections';
protected static string $tokenUrl = 'https://identity.xero.com/connect/token';
protected static string $revokeUrl = 'https://identity.xero.com/connect/revocation';
protected mixed $tenant_id = 0;
protected string $tenant_id = '';

public function setTenantId(string $tenant_id): void
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public function disconnect(): void
* @return RedirectResponse|Application|Redirector
* @throws Exception
*/
public function connect(): RedirectResponse|Application|Redirector
public function connect()
{
//when no code param redirect to Microsoft
if (request()->has('code')) {
Expand Down Expand Up @@ -136,7 +136,7 @@ public function connect(): RedirectResponse|Application|Redirector
return redirect()->away($url);
}

public function getTokenData(): XeroToken|null
public function getTokenData(): ?XeroToken
{
if ($this->tenant_id) {
$token = XeroToken::where('tenant_id', '=', $this->tenant_id)->first();
Expand Down Expand Up @@ -251,6 +251,8 @@ protected function redirectIfNoToken(string $token, bool $redirectWhenNotConnect
if (! $this->isConnected() && $redirectWhenNotConnected === true) {
return redirect()->away(config('xero.redirectUri'));
}

return false;
}

/**
Expand Down Expand Up @@ -299,10 +301,6 @@ protected function guzzle(string $type, string $request, array $data = [], bool
$data = null;
}

//contacts?where=ContactID==Guid("74ea95ea-6e1e-435d-9c30-0dff8ae1bd80

//dd([$request]);

try {
$response = Http::withToken($this->getAccessToken())
->withHeaders(array_merge(['Xero-tenant-id' => $this->getTenantId()], $headers))
Expand All @@ -316,7 +314,7 @@ protected function guzzle(string $type, string $request, array $data = [], bool
];
} catch (RequestException $e) {
$response = json_decode($e->response->body());
throw new Exception($response->Detail ?? "Type: {$response->Type} Message: {$response->Message} Error Number: {$response->ErrorNumber}");
throw new Exception($response->Detail ?? "Type: $response->Type Message: $response->Message Error Number: $response->ErrorNumber");
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
Expand All @@ -336,10 +334,6 @@ protected static function sendPost(string $url, array $params)
->acceptJson()
->post($url, $params);

//if ($response->status() !== 200 ) {
//throw new Exception($response->json()['error'] . ' - Try Refreshing Tokens, Error Code: ' . $response->status());
//}

return $response->json();

} catch (Exception $e) {
Expand Down

0 comments on commit 393d644

Please sign in to comment.