-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from dcblogdev/refactor-to-actions
refactored to actions
- Loading branch information
Showing
4 changed files
with
85 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '&'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]); |