OpenXE/classes/Modules/AmazonVendorDF/Data/Token.php
2021-05-21 08:49:41 +02:00

49 lines
1.2 KiB
PHP

<?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']);
}
}