Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Sep 10, 2024
1 parent 1b2651e commit 1ed0097
Show file tree
Hide file tree
Showing 28 changed files with 242 additions and 257 deletions.
18 changes: 9 additions & 9 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
/************************************************************************
* Client ID provided by Xero when registering your application
************************************************************************/
'client_id' => env('XERO_CLIENT_ID'),
'client_id' => env('XERO_CLIENT_ID'),

/************************************************************************
* Client Secret provided by Xero when registering your application
************************************************************************/
'client_secret' => env('XERO_CLIENT_SECRET'),
'client_secret' => env('XERO_CLIENT_SECRET'),

/************************************************************************
* Webhook signing key provided by Xero when registering webhooks
************************************************************************/
'webhook_signing_key' => env('XERO_WEBHOOK_KEY', ''),
'webhook_signing_key' => env('XERO_WEBHOOK_KEY', ''),

/************************************************************************
* Then scopes you wish to request access to on your token
* https://developer.xero.com/documentation/oauth2/scopes
************************************************************************/
'scopes' => [
'scopes' => [
'openid',
'email',
'profile',
Expand All @@ -48,26 +48,26 @@
/************************************************************************
* Url to redirect to upon success
************************************************************************/
'redirect_on_success' => 'xero.auth.success',
'redirect_on_success' => 'xero.auth.success',

/************************************************************************
* Url for Xero to redirect to upon granting access
* Unless you wish to change the default behaviour you should not need to
* change this
************************************************************************/
'redirect_uri' => 'xero.auth.callback',
'redirect_uri' => 'xero.auth.callback',

/************************************************************************
* If the 'redirect_uri' is not a route name, but rather a full url set
* this to true and we won't wrap it in `route()`
************************************************************************/
'redirect_full_url' => false,
'redirect_full_url' => false,

/************************************************************************
* Urls for Xero's Oauth integration, you shouldn't need to change these
************************************************************************/
'url_authorize' => 'https://login.xero.com/identity/connect/authorize',
'url_access_token' => 'https://identity.xero.com/connect/token',
'url_authorize' => 'https://login.xero.com/identity/connect/authorize',
'url_access_token' => 'https://identity.xero.com/connect/token',
'url_resource_owner_details' => 'https://api.xero.com/api.xro/2.0/Organisation',
],

Expand Down
8 changes: 4 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use \Illuminate\Support\Facades\Route;
use \Webfox\Xero\Controllers\AuthorizationController;
use \Webfox\Xero\Controllers\AuthorizationCallbackController;
use Illuminate\Support\Facades\Route;
use Webfox\Xero\Controllers\AuthorizationCallbackController;
use Webfox\Xero\Controllers\AuthorizationController;

Route::middleware('web')->group(function() {
Route::middleware('web')->group(function () {
Route::get('/xero/auth/authorize', AuthorizationController::class)->name('xero.auth.authorize');
Route::get('/xero/auth/callback', AuthorizationCallbackController::class)->name('xero.auth.callback');
});
3 changes: 1 addition & 2 deletions src/Clients/AccountAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class AccountAPIClient extends BaseClient
{

}
}
4 changes: 2 additions & 2 deletions src/Clients/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static function fake(): void

public static function getHttpClient(): Client|Mockery
{
if(self::$httpClient){
if (self::$httpClient) {
return self::$httpClient;
}

return self::$httpClient = new Client();
}
}
}
3 changes: 1 addition & 2 deletions src/Clients/IdentityAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class IdentityAPIClient extends BaseClient
{

}
}
24 changes: 12 additions & 12 deletions src/Controllers/AuthorizationCallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Webfox\Xero\Controllers;

use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Webfox\Xero\Oauth2Provider;
use Illuminate\Routing\Controller;
use XeroAPI\XeroPHP\Api\IdentityApi;
use Illuminate\Support\Facades\Event;
use Webfox\Xero\Events\XeroAuthorized;
use Webfox\Xero\OauthCredentialManager;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Str;
use Webfox\Xero\Events\XeroAuthorized;
use Webfox\Xero\Exceptions\OAuthException;
use Webfox\Xero\Oauth2Provider;
use Webfox\Xero\OauthCredentialManager;
use XeroAPI\XeroPHP\Api\IdentityApi;

class AuthorizationCallbackController extends Controller
{
Expand All @@ -24,8 +24,8 @@ public function __invoke(Request $request, OauthCredentialManager $oauth, Identi
$this->validate($request, [
'error' => ['sometimes', 'required', 'string'],
'error_description' => ['required_with:error', 'string'],
'code' => ['required_if:error,null', 'string'],
'state' => ['required', 'string', "in:{$oauth->getState()}"]
'code' => ['required_if:error,null', 'string'],
'state' => ['required', 'string', "in:{$oauth->getState()}"],
]);

if ($request->has('error')) {
Expand All @@ -41,16 +41,16 @@ public function __invoke(Request $request, OauthCredentialManager $oauth, Identi
}

$accessToken = $provider->getAccessToken('authorization_code', $request->only('code'));
$identity->getConfig()->setAccessToken((string)$accessToken->getToken());
$identity->getConfig()->setAccessToken((string) $accessToken->getToken());

//Iterate tenants
$tenants = array();
$tenants = [];

foreach ($identity->getConnections() as $c) {
$tenants[] = [
"Id" => $c->getTenantId(),
"Name" => $c->getTenantName(),
"ConnectionId" => $c->getId(),
'Id' => $c->getTenantId(),
'Name' => $c->getTenantName(),
'ConnectionId' => $c->getId(),
];
}

Expand Down
1 change: 0 additions & 1 deletion src/Controllers/AuthorizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public function __invoke(Redirector $redirect, OauthCredentialManager $oauth): R
{
return $redirect->to($oauth->getAuthorizationUrl());
}

}
8 changes: 4 additions & 4 deletions src/Events/XeroAuthorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class XeroAuthorized

public function __construct(public array $data)
{
$this->token = $data['token'];
$this->token = $data['token'];
$this->refresh_token = $data['refresh_token'];
$this->id_token = $data['id_token'];
$this->expires = $data['expires'];
$this->tenants = $data['tenants'];
$this->id_token = $data['id_token'];
$this->expires = $data['expires'];
$this->tenants = $data['tenants'];
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/OAuthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public function __construct($message, $code = 0, Throwable $previous = null)

public function __toString()
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
return __CLASS__.": [{$this->code}]: {$this->message}\n";
}
}
31 changes: 15 additions & 16 deletions src/Oauth2CredentialManagers/ArrayStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ArrayStore implements OauthCredentialManager

public function __construct(protected Store $session, protected Oauth2Provider $oauthProvider)
{

}

public function getAccessToken(): string
Expand All @@ -31,12 +30,12 @@ public function getTenants(): ?array
return $this->data('tenants');
}

public function getTenantId(int $tenant =0): string
public function getTenantId(int $tenant = 0): string
{
if(!isset($this->data('tenants')[$tenant]))
{
throw new \Exception("No such tenant exists");
if (! isset($this->data('tenants')[$tenant])) {
throw new \Exception('No such tenant exists');
}

return $this->data('tenants')[$tenant]['Id'];
}

Expand Down Expand Up @@ -86,11 +85,11 @@ public function refresh(): void
public function store(AccessTokenInterface $token, array $tenants = null): void
{
$this->dataStorage = [
'token' => $token->getToken(),
'token' => $token->getToken(),
'refresh_token' => $token->getRefreshToken(),
'id_token' => $token->getValues()['id_token'],
'expires' => $token->getExpires(),
'tenants' => $tenants ?? $this->getTenants()
'id_token' => $token->getValues()['id_token'],
'expires' => $token->getExpires(),
'tenants' => $tenants ?? $this->getTenants(),
];
}

Expand All @@ -102,12 +101,12 @@ public function getUser(): ?array
$decodedToken = $jwt->decode();

return [
'given_name' => $decodedToken->getGivenName(),
'given_name' => $decodedToken->getGivenName(),
'family_name' => $decodedToken->getFamilyName(),
'email' => $decodedToken->getEmail(),
'user_id' => $decodedToken->getXeroUserId(),
'username' => $decodedToken->getPreferredUsername(),
'session_id' => $decodedToken->getGlobalSessionId()
'email' => $decodedToken->getEmail(),
'user_id' => $decodedToken->getXeroUserId(),
'username' => $decodedToken->getPreferredUsername(),
'session_id' => $decodedToken->getGlobalSessionId(),
];
} catch (\Throwable $e) {
return null;
Expand All @@ -116,10 +115,10 @@ public function getUser(): ?array

protected function data(string $key = null)
{
if (!$this->exists()) {
if (! $this->exists()) {
throw new \Exception('Xero oauth credentials are missing');
}

return $key === null ? $this->dataStorage : $this->dataStorage[$key] ?? null;
}
}
}
36 changes: 17 additions & 19 deletions src/Oauth2CredentialManagers/CacheStore.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php


namespace Webfox\Xero\Oauth2CredentialManagers;


use Illuminate\Session\Store;
use Illuminate\Cache\Repository;
use Illuminate\Session\Store;
use League\OAuth2\Client\Token\AccessTokenInterface;
use Webfox\Xero\Oauth2Provider;
use Webfox\Xero\OauthCredentialManager;
Expand All @@ -16,7 +14,6 @@ class CacheStore implements OauthCredentialManager

public function __construct(protected Repository $cache, protected Store $session, protected Oauth2Provider $oauthProvider)
{

}

public function getAccessToken(): string
Expand All @@ -32,14 +29,14 @@ public function getRefreshToken(): string
public function getTenants(): ?array
{
return $this->data('tenants');
}
}

public function getTenantId(int $tenant =0): string
public function getTenantId(int $tenant = 0): string
{
if(!isset($this->data('tenants')[$tenant]))
{
throw new \Exception("No such tenant exists");
if (! isset($this->data('tenants')[$tenant])) {
throw new \Exception('No such tenant exists');
}

return $this->data('tenants')[$tenant]['Id'];
}

Expand Down Expand Up @@ -89,11 +86,11 @@ public function refresh(): void
public function store(AccessTokenInterface $token, array $tenants = null): void
{
$this->cache->forever($this->cacheKey, [
'token' => $token->getToken(),
'token' => $token->getToken(),
'refresh_token' => $token->getRefreshToken(),
'id_token' => $token->getValues()['id_token'],
'expires' => $token->getExpires(),
'tenants' => $tenants ?? $this->getTenants()
'id_token' => $token->getValues()['id_token'],
'expires' => $token->getExpires(),
'tenants' => $tenants ?? $this->getTenants(),
]);
}

Expand All @@ -105,12 +102,12 @@ public function getUser(): ?array
$decodedToken = $jwt->decode();

return [
'given_name' => $decodedToken->getGivenName(),
'given_name' => $decodedToken->getGivenName(),
'family_name' => $decodedToken->getFamilyName(),
'email' => $decodedToken->getEmail(),
'user_id' => $decodedToken->getXeroUserId(),
'username' => $decodedToken->getPreferredUsername(),
'session_id' => $decodedToken->getGlobalSessionId()
'email' => $decodedToken->getEmail(),
'user_id' => $decodedToken->getXeroUserId(),
'username' => $decodedToken->getPreferredUsername(),
'session_id' => $decodedToken->getGlobalSessionId(),
];
} catch (\Throwable $e) {
return null;
Expand All @@ -119,11 +116,12 @@ public function getUser(): ?array

protected function data(string $key = null)
{
if (!$this->exists()) {
if (! $this->exists()) {
throw new \Exception('Xero oauth credentials are missing');
}

$cacheData = $this->cache->get($this->cacheKey);

return empty($key) ? $cacheData : ($cacheData[$key] ?? null);
}
}
Loading

0 comments on commit 1ed0097

Please sign in to comment.