Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\GoogleApi\Data;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Xentral\Modules\GoogleApi\Exception\InvalidArgumentException;
|
||||
|
||||
final class GoogleAccessTokenData
|
||||
{
|
||||
/** @var int $accountId */
|
||||
private $accountId;
|
||||
|
||||
/** @var string $token */
|
||||
private $token;
|
||||
|
||||
/** @var DateTimeInterface $expirationDate */
|
||||
private $expirationDate;
|
||||
|
||||
/**
|
||||
* @param int $accountId
|
||||
* @param string $token
|
||||
* @param DateTimeInterface $expirationDate
|
||||
*/
|
||||
public function __construct(
|
||||
int $accountId,
|
||||
string $token,
|
||||
DateTimeInterface $expirationDate
|
||||
) {
|
||||
$this->accountId = $accountId;
|
||||
$this->token = $token;
|
||||
$this->expirationDate = $expirationDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return GoogleAccessTokenData
|
||||
*/
|
||||
public static function fromDbState($data): GoogleAccessTokenData
|
||||
{
|
||||
if (!isset($data['google_account_id'], $data['token'], $data['expires'])) {
|
||||
throw new InvalidArgumentException('Invalid Token Data.');
|
||||
}
|
||||
$expires = DateTime::createFromFormat('Y-m-d H:i:s', $data['expires']);
|
||||
|
||||
return new static($data['google_account_id'], $data['token'], $expires);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$expiration = null;
|
||||
if ($this->expirationDate !== null) {
|
||||
$expiration = $this->getExpirationDate()->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
return [
|
||||
'google_account_id' => $this->getAccountId(),
|
||||
'token' => $this->getToken(),
|
||||
'expires' => $expiration,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeToLive(): int
|
||||
{
|
||||
$now = new DateTime();
|
||||
$diff = $this->expirationDate->getTimestamp() - $now->getTimestamp();
|
||||
if ($diff < 0) {
|
||||
$diff = 0;
|
||||
}
|
||||
|
||||
return $diff;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAccountId(): int
|
||||
{
|
||||
return $this->accountId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTimeInterface
|
||||
*/
|
||||
public function getExpirationDate(): DateTimeInterface
|
||||
{
|
||||
return $this->expirationDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\GoogleApi\Data;
|
||||
|
||||
use Xentral\Modules\GoogleApi\Exception\InvalidArgumentException;
|
||||
|
||||
final class GoogleAccountData
|
||||
{
|
||||
/** @var int */
|
||||
private $id;
|
||||
|
||||
/** @var int */
|
||||
private $userId;
|
||||
|
||||
/** @var string $refreshToken */
|
||||
private $refreshToken;
|
||||
|
||||
/** @var string $identifier */
|
||||
private $identifier;
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @param int $userId
|
||||
* @param string|null $identifier
|
||||
* @param string|null $refreshToken
|
||||
*/
|
||||
public function __construct(
|
||||
?int $id,
|
||||
int $userId,
|
||||
?string $identifier,
|
||||
string $refreshToken = null
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->userId = $userId;
|
||||
$this->identifier = $identifier;
|
||||
$this->refreshToken = $refreshToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $dataSet
|
||||
*
|
||||
* @return GoogleAccountData
|
||||
*/
|
||||
public static function fromDbState($dataSet): GoogleAccountData
|
||||
{
|
||||
if (!isset($dataSet['user_id'], $dataSet['id'])) {
|
||||
throw new InvalidArgumentException('Invalid or incomplete Dataset.');
|
||||
}
|
||||
|
||||
return new self(
|
||||
$dataSet['id'],
|
||||
$dataSet['user_id'],
|
||||
$dataSet['identifier'],
|
||||
$dataSet['refresh_token']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'user_id' => $this->getUserId(),
|
||||
'identifier' => $this->getIdentifier(),
|
||||
'refresh_token' => $this->getRefreshToken(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getUserId(): int
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRefreshToken(): ?string
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getIdentifier(): ?string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\GoogleApi\Data;
|
||||
|
||||
use Countable;
|
||||
|
||||
final class GoogleAccountPropertyCollection implements Countable
|
||||
{
|
||||
/** @var GoogleAccountPropertyValue[] $properties */
|
||||
private $properties;
|
||||
|
||||
/**
|
||||
* @param GoogleAccountPropertyValue[] $properties
|
||||
*/
|
||||
public function __construct(array $properties = [])
|
||||
{
|
||||
$this->properties = [];
|
||||
foreach ($properties as $property) {
|
||||
$this->add($property);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->properties) && $this->properties[$key] !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function get(string $key): ?string
|
||||
{
|
||||
if (!array_key_exists($key, $this->properties) || $this->properties[$key] === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->properties[$key]->getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets property immutable
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @return GoogleAccountPropertyCollection
|
||||
*/
|
||||
public function set(string $key, string $value): GoogleAccountPropertyCollection
|
||||
{
|
||||
if ($this->has($key)) {
|
||||
$property = new GoogleAccountPropertyValue(
|
||||
$this->properties[$key]->getId(),
|
||||
$this->properties[$key]->getAccountId(),
|
||||
$this->properties[$key]->getKey(),
|
||||
$value
|
||||
);
|
||||
} else {
|
||||
$property = new GoogleAccountPropertyValue(null, null, $key, $value);
|
||||
}
|
||||
$properties = clone($this);
|
||||
$properties->properties[$key] = $property;
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes property immutable
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
* @return GoogleAccountPropertyCollection
|
||||
*/
|
||||
public function remove(string $key): GoogleAccountPropertyCollection
|
||||
{
|
||||
if (!array_key_exists($key, $this->properties)) {
|
||||
return $this;
|
||||
}
|
||||
$properties = clone($this);
|
||||
$properties->properties[$key] = null;
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return GoogleAccountPropertyValue[]
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets properties as Key => Value array
|
||||
*
|
||||
* @return array [key => value]
|
||||
*/
|
||||
public function getkeyValueMap(): array
|
||||
{
|
||||
$array = [];
|
||||
foreach ($this->properties as $key => $obj) {
|
||||
if ($obj !== null) {
|
||||
$array[$key] = $obj->getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
$count = 0;
|
||||
foreach ($this->properties as $property) {
|
||||
if ($property !== null) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __debugInfo(): ?array
|
||||
{
|
||||
return $this->getkeyValueMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GoogleAccountPropertyValue $property
|
||||
*/
|
||||
private function add(GoogleAccountPropertyValue $property): void
|
||||
{
|
||||
$this->properties[$property->getKey()] = $property;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\GoogleApi\Data;
|
||||
|
||||
use Xentral\Modules\GoogleApi\Exception\InvalidArgumentException;
|
||||
|
||||
final class GoogleAccountPropertyValue
|
||||
{
|
||||
/** @var string $key */
|
||||
private $key;
|
||||
|
||||
/** @var string $value */
|
||||
private $value;
|
||||
|
||||
/** @var int|null */
|
||||
private $id;
|
||||
|
||||
/** @var int|null */
|
||||
private $accountId;
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @param int $accountId
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function __construct(?int $id, ?int $accountId, string $key, string $value)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->accountId = $accountId;
|
||||
$this->key = $key;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return GoogleAccountPropertyValue
|
||||
*/
|
||||
public static function fromDbState(array $data): GoogleAccountPropertyValue
|
||||
{
|
||||
if (!isset($data['id'], $data['google_account_id'], $data['varname'], $data['value'])) {
|
||||
throw new InvalidArgumentException('Invalid or incomplete Dataset.');
|
||||
}
|
||||
|
||||
return new static($data['id'], $data['google_account_id'], $data['varname'], $data['value']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'google_account_id' => $this->getAccountId(),
|
||||
'varname' => $this->getKey(),
|
||||
'value' => $this->getValue()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getAccountId(): ?int
|
||||
{
|
||||
return $this->accountId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\GoogleApi\Data;
|
||||
|
||||
use Xentral\Modules\GoogleApi\Exception\GoogleCredentialsException;
|
||||
|
||||
final class GoogleCredentialsData
|
||||
{
|
||||
/** @var string|null $clientId */
|
||||
private $clientId;
|
||||
|
||||
/** @var string|null $clientSecret */
|
||||
private $clientSecret;
|
||||
|
||||
/** @var string|null $redirectUri */
|
||||
private $redirectUri;
|
||||
|
||||
/**
|
||||
* @param string $clientId
|
||||
* @param string $clientSecret
|
||||
* @param string $redirectUri
|
||||
*/
|
||||
public function __construct(
|
||||
?string $clientId,
|
||||
?string $clientSecret,
|
||||
?string $redirectUri
|
||||
) {
|
||||
$this->clientId = $clientId;
|
||||
$this->clientSecret = $clientSecret;
|
||||
$this->redirectUri = $redirectUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getClientId(): ?string
|
||||
{
|
||||
return $this->clientId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getClientSecret(): ?string
|
||||
{
|
||||
return $this->clientSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRedirectUri(): ?string
|
||||
{
|
||||
return $this->redirectUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GoogleCredentialsException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function validate(): void
|
||||
{
|
||||
if (empty($this->getClientId())) {
|
||||
throw new GoogleCredentialsException('Google client-id not set.');
|
||||
}
|
||||
if (empty($this->getClientSecret())) {
|
||||
throw new GoogleCredentialsException('Google client secret not set.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\GoogleApi\Data;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Xentral\Modules\GoogleApi\Exception\InvalidArgumentException;
|
||||
|
||||
final class GoogleTokenResponseData
|
||||
{
|
||||
/** @var string $accessToken */
|
||||
private $accessToken;
|
||||
|
||||
/** @var string[] $scopes */
|
||||
private $scopes;
|
||||
|
||||
/** @var string|null $tokenType */
|
||||
private $tokenType;
|
||||
|
||||
/** @var string|null $refreshToken */
|
||||
private $refreshToken;
|
||||
|
||||
/** @var DateTimeImmutable $expirationDate */
|
||||
private $expirationDate;
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
* @param int $expiresIn
|
||||
* @param array $scopes
|
||||
* @param string|null $tokenType
|
||||
* @param string|null $refreshToken
|
||||
*/
|
||||
private function __construct(
|
||||
string $accessToken,
|
||||
int $expiresIn,
|
||||
array $scopes,
|
||||
string $tokenType,
|
||||
?string $refreshToken = null
|
||||
) {
|
||||
$this->accessToken = $accessToken;
|
||||
$this->scopes = $scopes;
|
||||
$this->tokenType = $tokenType;
|
||||
$this->refreshToken = $refreshToken;
|
||||
$this->setExpiration($expiresIn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return GoogleTokenResponseData
|
||||
*/
|
||||
public static function createfromResponseArray(array $data): GoogleTokenResponseData
|
||||
{
|
||||
if (!isset($data['access_token'], $data['expires_in'], $data['scope'], $data['token_type'])) {
|
||||
throw new InvalidArgumentException('Invalid token response.');
|
||||
}
|
||||
$scopes = explode(' ', $data['scope']);
|
||||
$obj = new static(
|
||||
$data['access_token'],
|
||||
$data['expires_in'],
|
||||
$scopes,
|
||||
$data['token_type']
|
||||
);
|
||||
if (array_key_exists('token_type', $data)) {
|
||||
$obj->tokenType = $data['token_type'];
|
||||
}
|
||||
if (array_key_exists('refresh_token', $data)) {
|
||||
$obj->refreshToken = $data['refresh_token'];
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRefreshToken(): bool
|
||||
{
|
||||
return !empty($this->refreshToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTimeInterface
|
||||
*/
|
||||
public function getExpirationDate(): DateTimeInterface
|
||||
{
|
||||
return $this->expirationDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getScopes(): array
|
||||
{
|
||||
return $this->scopes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTokenType(): string
|
||||
{
|
||||
return $this->tokenType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRefreshToken(): ?string
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $expiresIn
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function setExpiration(int $expiresIn): void
|
||||
{
|
||||
$this->expirationDate = new DateTimeImmutable('now');
|
||||
$interval = new DateInterval(sprintf('PT%sS', $expiresIn));
|
||||
$this->expirationDate = $this->expirationDate->add($interval);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user