Skip to content

Commit

Permalink
Move data -> data transfer objects
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbrink committed Apr 15, 2024
1 parent 3e0924a commit 2bd30f9
Show file tree
Hide file tree
Showing 660 changed files with 2,258 additions and 2,287 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix="ListSchema.php">./src/DataTransferObjects</directory>
<directory suffix="ListSchema.php">./src/Data</directory>
</exclude>
</source>
</phpunit>
31 changes: 15 additions & 16 deletions src/AmznSPAConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Jasara\AmznSPA;

use Carbon\CarbonImmutable;
use Closure;
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Client\PendingRequest;
use Jasara\AmznSPA\Constants\Marketplace;
use Jasara\AmznSPA\Constants\MarketplacesList;
use Jasara\AmznSPA\DataTransferObjects\ApplicationKeysDTO;
use Jasara\AmznSPA\DataTransferObjects\AuthTokensDTO;
use Jasara\AmznSPA\DataTransferObjects\GrantlessTokenDTO;
use Jasara\AmznSPA\DataTransferObjects\RestrictedDataTokenDTO;
use Jasara\AmznSPA\Data\ApplicationKeysDTO;
use Jasara\AmznSPA\Data\AuthTokensDTO;
use Jasara\AmznSPA\Data\GrantlessTokenDTO;
use Jasara\AmznSPA\Data\RestrictedDataTokenDTO;
use Jasara\AmznSPA\Traits\ValidatesParameters;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Log\Logger;
Expand Down Expand Up @@ -40,9 +39,9 @@ public function __construct(
private ?string $redirect_url = null,
private bool $use_test_endpoints = false,
private ?bool $get_rdt_tokens = true,
private ?Closure $save_lwa_tokens_callback = null,
private ?Closure $authentication_exception_callback = null,
private ?Closure $response_callback = null,
private ?\Closure $save_lwa_tokens_callback = null,
private ?\Closure $authentication_exception_callback = null,
private ?\Closure $response_callback = null,
?LoggerInterface $logger = null,
?string $aws_access_key = null,
?string $aws_secret_key = null,
Expand All @@ -58,7 +57,7 @@ public function __construct(
) {
$this->validateStringEnum($marketplace_id, MarketplacesList::allIdentifiers());

$this->http = (new Factory)->connectTimeout(10);
$this->http = (new Factory())->connectTimeout(10);
$this->tokens = new AuthTokensDTO(
refresh_token: $lwa_refresh_token,
access_token: $lwa_access_token,
Expand Down Expand Up @@ -119,17 +118,17 @@ public function getRedirectUrl(): string
return $this->redirect_url;
}

public function getSaveLwaTokensCallback(): Closure
public function getSaveLwaTokensCallback(): \Closure
{
return $this->save_lwa_tokens_callback;
}

public function getAuthenticationExceptionCallback(): Closure
public function getAuthenticationExceptionCallback(): \Closure
{
return $this->authentication_exception_callback;
}

public function getResponseCallback(): Closure
public function getResponseCallback(): \Closure
{
return $this->response_callback;
}
Expand Down Expand Up @@ -175,17 +174,17 @@ public function setGrantlessToken(GrantlessTokenDTO $token): void
$this->grantless_token = $token;
}

public function setSaveLwaTokensCallback(Closure $callback): void
public function setSaveLwaTokensCallback(\Closure $callback): void
{
$this->save_lwa_tokens_callback = $callback;
}

public function setAuthenticationExceptionCallback(Closure $callback): void
public function setAuthenticationExceptionCallback(\Closure $callback): void
{
$this->authentication_exception_callback = $callback;
}

public function setResponseCallback(Closure $callback): void
public function setResponseCallback(\Closure $callback): void
{
$this->response_callback = $callback;
}
Expand All @@ -209,6 +208,6 @@ public function setMarketplace(string $marketplace_id): void

public function isPropertySet(string $property): bool
{
return isset($this->$property) && ! is_null($this->$property);
return isset($this->$property) && !is_null($this->$property);
}
}
47 changes: 21 additions & 26 deletions src/AmznSPAHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Jasara\AmznSPA\Constants\JasaraNotes;
use Jasara\AmznSPA\DataTransferObjects\Requests\Tokens\CreateRestrictedDataTokenRequest;
use Jasara\AmznSPA\DataTransferObjects\RestrictedDataTokenDTO;
use Jasara\AmznSPA\DataTransferObjects\Schemas\MetadataSchema;
use Jasara\AmznSPA\Data\Requests\Tokens\CreateRestrictedDataTokenRequest;
use Jasara\AmznSPA\Data\RestrictedDataTokenDTO;
use Jasara\AmznSPA\Data\Schemas\MetadataSchema;
use Jasara\AmznSPA\Exceptions\AmznSPAException;
use Jasara\AmznSPA\Exceptions\AuthenticationException;
use Jasara\AmznSPA\Exceptions\GrantlessAuthenticationException;
Expand Down Expand Up @@ -133,15 +133,10 @@ private function call(string $method, string $url, array $data = [], bool $grant

if ($this->isAuthenticationException($e)) {
if ($grantless) {
throw new GrantlessAuthenticationException(
$e->response,
);
throw new GrantlessAuthenticationException($e->response);
}

throw new AuthenticationException(
$e->response,
$this->config->isPropertySet('authentication_exception_callback') ? $this->config->getAuthenticationExceptionCallback() : null,
);
throw new AuthenticationException($e->response, $this->config->isPropertySet('authentication_exception_callback') ? $this->config->getAuthenticationExceptionCallback() : null);
}

if ($e->response->status() === 429) {
Expand Down Expand Up @@ -188,7 +183,7 @@ private function refreshTokens(): void

$refresh_token = $this->config->getTokens()->refresh_token;

if (! $refresh_token) {
if (!$refresh_token) {
throw new AmznSPAException('Refresh token is not set');
}

Expand All @@ -199,7 +194,7 @@ private function refreshTokens(): void

private function refreshGrantlessToken()
{
$scope = 'sellingpartnerapi::' . $this->grantless_resource;
$scope = 'sellingpartnerapi::'.$this->grantless_resource;

$amzn = new AmznSPA($this->config);
$token = $amzn->lwa->getGrantlessAccessToken($scope);
Expand Down Expand Up @@ -254,23 +249,23 @@ private function getToken(bool $grantless, string $url, string $method): string
if ($this->shouldGetRestrictedDataToken($url, $method)) {
$restricted_token = $this->config->getRestrictedDataToken();

if (! $this->isRestrictedTokenCompatibleWithPath($restricted_token, $url)) {
if (!$this->isRestrictedTokenCompatibleWithPath($restricted_token, $url)) {
$this->refreshRdtToken($url, $method);
} elseif (! $restricted_token->access_token || ($restricted_token->expires_at && $restricted_token->expires_at->subMinutes(5)->isPast())) {
} elseif (!$restricted_token->access_token || ($restricted_token->expires_at && $restricted_token->expires_at->subMinutes(5)->isPast())) {
$this->refreshRdtToken($url, $method);
}

return $this->config->getRestrictedDataToken()->access_token;
} elseif (! $grantless) {
} elseif (!$grantless) {
$tokens = $this->config->getTokens();
if (! $tokens->access_token || ($tokens->expires_at && $tokens->expires_at->subMinutes(5)->isPast())) {
if (!$tokens->access_token || ($tokens->expires_at && $tokens->expires_at->subMinutes(5)->isPast())) {
$this->refreshTokens();
}

return $this->config->getTokens()->access_token;
} else {
$grantless_token = $this->config->getGrantlessToken();
if (! $grantless_token->access_token || ($grantless_token->expires_at && $grantless_token->expires_at->subMinutes(5)->isPast())) {
if (!$grantless_token->access_token || ($grantless_token->expires_at && $grantless_token->expires_at->subMinutes(5)->isPast())) {
$this->refreshGrantlessToken();
}

Expand All @@ -280,15 +275,15 @@ private function getToken(bool $grantless, string $url, string $method): string

private function shouldGetRestrictedDataToken(string $url, string $method): bool
{
if (! $this->config->shouldGetRdtTokens()) {
if (!$this->config->shouldGetRdtTokens()) {
return false;
}

if (! $this->isRestrictedDataPath($url, $method)) {
if (!$this->isRestrictedDataPath($url, $method)) {
return false;
}

if (! $this->use_restricted_data_token) {
if (!$this->use_restricted_data_token) {
return false;
}

Expand Down Expand Up @@ -380,18 +375,18 @@ private function callResponseCallback(Response $response): void

private function handleRequestException(RequestException $e, bool $grantless)
{
if (! $e->response->json()) {
if (!$e->response->json()) {
throw $e;
}

if (! $this->shouldRefreshToken($e->response->json())) {
if (!$this->shouldRefreshToken($e->response->json())) {
throw $e;
}
if (! $this->shouldRetry()) {
if (!$this->shouldRetry()) {
throw $e;
}

if (! $grantless) {
if (!$grantless) {
$this->refreshTokens();
} else {
$this->refreshGrantlessToken();
Expand All @@ -400,11 +395,11 @@ private function handleRequestException(RequestException $e, bool $grantless)

public function shouldReturnErrorResponse(RequestException $e): bool
{
if (! in_array($e->response->status(), [400, 404])) {
if (!in_array($e->response->status(), [400, 404])) {
return false;
}

if (! Arr::get($e->response->json(), 'errors')) {
if (!Arr::get($e->response->json(), 'errors')) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects;
namespace Jasara\AmznSPA\Data;

use Spatie\DataTransferObject\DataTransferObject;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects;
namespace Jasara\AmznSPA\Data;

use Carbon\CarbonImmutable;
use Jasara\AmznSPA\DataTransferObjects\Casts\CarbonFromSecondsCaster;
use Jasara\AmznSPA\Data\Casts\CarbonFromSecondsCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\DataTransferObject;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Casts;
namespace Jasara\AmznSPA\Data\Casts;

use BackedEnum;
use Spatie\DataTransferObject\Caster;

class BackedEnumCaster implements Caster
Expand All @@ -14,20 +13,20 @@ public function __construct(
) {
$this->enum_class = $caster_arguments[0];

if (! enum_exists($this->enum_class)) {
if (!enum_exists($this->enum_class)) {
throw new \Exception('The provided class is not an enum');
}
}

public function cast(mixed $value): BackedEnum
public function cast(mixed $value): \BackedEnum
{
if ($value instanceof BackedEnum) {
if ($value instanceof \BackedEnum) {
if (get_class($value) === $this->enum_class) {
return $value;
}
}

if (! is_string($value) && ! is_int($value)) {
if (!is_string($value) && !is_int($value)) {
throw new \Exception('Cannot cast values that are not strings or integers to a backed Enum');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Casts;
namespace Jasara\AmznSPA\Data\Casts;

use Carbon\CarbonImmutable;
use Spatie\DataTransferObject\Caster;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Casts;
namespace Jasara\AmznSPA\Data\Casts;

use Carbon\CarbonImmutable;
use Spatie\DataTransferObject\Caster;
Expand Down
8 changes: 8 additions & 0 deletions src/Data/Casts/CarbonToDateStringMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Jasara\AmznSPA\Data\Casts;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class CarbonToDateStringMapper
{
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects;
namespace Jasara\AmznSPA\Data;

use Carbon\CarbonImmutable;
use Jasara\AmznSPA\DataTransferObjects\Casts\CarbonFromSecondsCaster;
use Jasara\AmznSPA\Data\Casts\CarbonFromSecondsCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\DataTransferObject;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Requests;
namespace Jasara\AmznSPA\Data\Requests;

use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Jasara\AmznSPA\Contracts\PascalCaseRequestContract;
use Jasara\AmznSPA\DataTransferObjects\Casts\CarbonToDateStringMapper;
use Jasara\AmznSPA\Data\Casts\CarbonToDateStringMapper;
use Spatie\DataTransferObject\DataTransferObject;

class BaseRequest extends DataTransferObject
Expand Down
10 changes: 10 additions & 0 deletions src/Data/Requests/Feeds/CreateFeedDocumentSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Jasara\AmznSPA\Data\Requests\Feeds;

use Jasara\AmznSPA\Data\Requests\BaseRequest;

class CreateFeedDocumentSpecification extends BaseRequest
{
public string $content_type;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Requests\Feeds;
namespace Jasara\AmznSPA\Data\Requests\Feeds;

use Jasara\AmznSPA\Constants\AmazonEnums;
use Jasara\AmznSPA\DataTransferObjects\Requests\BaseRequest;
use Jasara\AmznSPA\DataTransferObjects\Validators\StringEnumValidator;
use Jasara\AmznSPA\Data\Requests\BaseRequest;
use Jasara\AmznSPA\Data\Validators\StringEnumValidator;

class CreateFeedSpecification extends BaseRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Jasara\AmznSPA\DataTransferObjects\Requests\FulfillmentInbound;
namespace Jasara\AmznSPA\Data\Requests\FulfillmentInbound;

use Jasara\AmznSPA\Contracts\PascalCaseRequestContract;
use Jasara\AmznSPA\DataTransferObjects\Requests\BaseRequest;
use Jasara\AmznSPA\DataTransferObjects\Schemas\AddressSchema;
use Jasara\AmznSPA\DataTransferObjects\Schemas\FulfillmentInbound\InboundShipmentPlanRequestItemListSchema;
use Jasara\AmznSPA\DataTransferObjects\Schemas\FulfillmentInbound\InboundShipmentPlanRequestItemSchema;
use Jasara\AmznSPA\DataTransferObjects\Validators\StringEnumValidator;
use Jasara\AmznSPA\Data\Requests\BaseRequest;
use Jasara\AmznSPA\Data\Schemas\AddressSchema;
use Jasara\AmznSPA\Data\Schemas\FulfillmentInbound\InboundShipmentPlanRequestItemListSchema;
use Jasara\AmznSPA\Data\Schemas\FulfillmentInbound\InboundShipmentPlanRequestItemSchema;
use Jasara\AmznSPA\Data\Validators\StringEnumValidator;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Casters\ArrayCaster;

Expand Down
Loading

0 comments on commit 2bd30f9

Please sign in to comment.