-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
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,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Adbros\Shoptet\Entity; | ||
|
||
class ApiAccessToken | ||
{ | ||
|
||
public function __construct( | ||
public readonly string $accessToken, | ||
public readonly int $expiresIn, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<string,mixed> $json | ||
*/ | ||
public static function fromJson(array $json): self | ||
{ | ||
return new self( | ||
accessToken: $json['access_token'], | ||
expiresIn: $json['expires_in'], | ||
); | ||
} | ||
|
||
} |
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,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Adbros\Shoptet\Entity; | ||
|
||
class OAuthAccessToken | ||
{ | ||
|
||
public function __construct( | ||
public readonly string $accessToken, | ||
public readonly ?int $expiresIn, | ||
public readonly string $tokenType, | ||
public readonly string $scope, | ||
public readonly int $eshopId, | ||
public readonly string $eshopUrl, | ||
public readonly string $contactEmail, | ||
public readonly bool $trial, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param array<string,mixed> $json | ||
*/ | ||
public static function fromJson(array $json): self | ||
{ | ||
return new self( | ||
accessToken: $json['access_token'], | ||
expiresIn: $json['expires_in'], | ||
tokenType: $json['token_type'], | ||
scope: $json['scope'], | ||
eshopId: $json['eshopId'], | ||
eshopUrl: $json['eshopUrl'], | ||
contactEmail: $json['contactEmail'], | ||
trial: $json['trial'], | ||
); | ||
} | ||
|
||
} |
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,76 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Adbros\Shoptet; | ||
|
||
use Adbros\Shoptet\Entity\ApiAccessToken; | ||
use Adbros\Shoptet\Entity\OAuthAccessToken; | ||
use Adbros\Shoptet\Exception\ClientException; | ||
use Adbros\Shoptet\Exception\ServerException; | ||
use GuzzleHttp\Client; | ||
use Nette\Utils\Json; | ||
|
||
class OAuth | ||
{ | ||
|
||
private Client $httpClient; | ||
|
||
public function __construct( | ||
private readonly string $clientId, | ||
private readonly string $clientSecret, | ||
private readonly string $oAuthAccessTokenUrl, | ||
private readonly string $apiAccessTokenUrl, | ||
) | ||
{ | ||
$this->httpClient = new Client(); | ||
} | ||
|
||
public function getOAuthAccessToken(string $code, string $redirectUri): OAuthAccessToken // @phpcs:ignore Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps | ||
{ | ||
/** @var array<string, mixed> $response */ | ||
$response = $this->request('POST', $this->oAuthAccessTokenUrl, [ | ||
'json' => [ | ||
'client_id' => $this->clientId, | ||
'client_secret' => $this->clientSecret, | ||
'code' => $code, | ||
'grant_type' => 'authorization_code', | ||
'redirect_uri' => $redirectUri, | ||
'scope' => 'api', | ||
], | ||
]); | ||
|
||
return OAuthAccessToken::fromJson($response); | ||
} | ||
|
||
public function getApiAccessToken(string $oAuthAccessToken): ApiAccessToken | ||
{ | ||
/** @var array<string, mixed> $response */ | ||
$response = $this->request('POST', $this->apiAccessTokenUrl, [ | ||
'headers' => [ | ||
'Authorization' => sprintf('Bearer %s', $oAuthAccessToken), | ||
], | ||
]); | ||
|
||
return ApiAccessToken::fromJson($response); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $options | ||
* @return array<string, mixed> | ||
*/ | ||
private function request(string $method, string $uri, array $options = []): ?array | ||
{ | ||
try { | ||
$response = $this->httpClient->request($method, $uri, $options); | ||
} catch (\GuzzleHttp\Exception\ClientException | \GuzzleHttp\Exception\ServerException $e) { | ||
$class = $e instanceof \GuzzleHttp\Exception\ClientException | ||
? ClientException::class | ||
: ServerException::class; | ||
$json = Json::decode($e->getResponse()->getBody()->getContents(), Json::FORCE_ARRAY); | ||
|
||
throw new $class('', $e->getCode(), $e, $json['errors'] ?? []); | ||
} | ||
|
||
return Json::decode($response->getBody()->getContents(), Json::FORCE_ARRAY); | ||
} | ||
|
||
} |
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