Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored to actions #75

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Actions/StoreTokenAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Dcblogdev\Xero\Actions;

use Dcblogdev\Xero\Models\XeroToken;
use Illuminate\Support\Facades\Crypt;

class StoreTokenAction
{
public function __invoke(array $token, array $tenantData = [], string $tenantId = null): XeroToken
{
$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 ($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);
}
}
17 changes: 17 additions & 0 deletions src/Actions/formatQueryStringsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dcblogdev\Xero\Actions;

class formatQueryStringsAction
{
public function __invoke(array $params): string
{
$queryString = '';

foreach ($params as $key => $value) {
$queryString .= "$key=$value&";
}

return rtrim($queryString, '&');
}
}
41 changes: 5 additions & 36 deletions src/Xero.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}
31 changes: 31 additions & 0 deletions tests/Actions/formatQueryStringsActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Dcblogdev\Xero\Actions\formatQueryStringsAction;

test('format query strings action', function () {

$params = [
'key1' => '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(),
]);