Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Data;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Token
|
||||
{
|
||||
/** @var string */
|
||||
private $accessToken;
|
||||
|
||||
/** @var string */
|
||||
private $refreshToken;
|
||||
|
||||
/** @var DateTimeImmutable */
|
||||
private $expirationDate;
|
||||
|
||||
public function __construct(string $accessToken, string $refreshToken, int $expiresInSeconds = 3600)
|
||||
{
|
||||
$this->accessToken = $accessToken;
|
||||
$this->refreshToken = $refreshToken;
|
||||
$this->expirationDate = new DateTimeImmutable( "+{$expiresInSeconds} seconds");
|
||||
}
|
||||
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
public function getRefreshToken(): string
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
public function isExpired(): bool
|
||||
{
|
||||
return new DateTime('now') > $this->expirationDate;
|
||||
}
|
||||
|
||||
public static function fromResponse(ResponseInterface $response): self
|
||||
{
|
||||
$tokenInformation = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
return new static($tokenInformation['access_token'], $tokenInformation['refresh_token'], $tokenInformation['expires_in']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user