Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\GoogleApi\Client;
use Xentral\Components\HttpClient\Exception\TransferErrorExceptionInterface;
use Xentral\Components\HttpClient\HttpClientInterface;
use Xentral\Components\HttpClient\Request\ClientRequest;
use Xentral\Components\Logger\LoggerAwareTrait;
use Xentral\Components\Util\StringUtil;
use Xentral\Modules\GoogleApi\Data\GoogleAccountData;
use Xentral\Modules\GoogleApi\Exception\GoogleApiRequestException;
use Xentral\Modules\GoogleApi\Exception\GoogleApiResponseException;
final class GoogleApiClient implements GoolgeApiClientInterface
{
use LoggerAwareTrait;
/** @var HttpClientInterface $httpClient */
private $httpClient;
/** @var GoogleAccountData $account */
private $account;
/**
* @param HttpClientInterface $client
* @param GoogleAccountData $account
*/
public function __construct(HttpClientInterface $client, GoogleAccountData $account)
{
$this->httpClient = $client;
$this->account = $account;
}
/**
* @return GoogleAccountData
*/
public function getAccount(): GoogleAccountData
{
return $this->account;
}
/**
* @param string $method
* @param string $uri
* @param array $data
* @param array $headers
*
* @throws GoogleApiRequestException
* @throws GoogleApiResponseException
*
* @return array
*/
public function sendRequest(
string $method,
string $uri,
array $data = null,
array $headers = []
): array {
$requestBody = null;
if ($data !== null && is_array($data) && count($data) > 0) {
$headers['Content-Type'] = 'application/json';
$requestBody = json_encode($data);
}
$request = new ClientRequest($method, $uri, $headers, $requestBody);
try {
$response = $this->httpClient->sendRequest($request);
$this->logger->debug(
'Google API request succeeded: {uri}',
['uri' => $request->getUri(), 'request' => $request, 'response' => $response]
);
} catch (TransferErrorExceptionInterface $e) {
$this->logger->warning(
'Google API request failed: {uri} ERROR {code}',
[
'uri' => $request->getUri(),
'code' => $e->getCode(),
'request' => $request,
'response' => $e->getResponse(),
]
);
throw new GoogleApiRequestException($e->getMessage(), $e->getCode(), $e);
}
$result = [];
$contentType = mb_strtolower($response->getHeaderLine('content-type'));
$responseBody = $response->getBody()->getContents();
if ($responseBody !== '' && StringUtil::startsWith($contentType, 'application/json')) {
$result = json_decode($responseBody, true);
}
if ($result === false || $result === null || !is_array($result)) {
throw new GoogleApiResponseException('Wrong format in JSON response.');
}
return $result;
}
}
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\GoogleApi\Client;
use Xentral\Components\HttpClient\HttpClientFactory;
use Xentral\Components\HttpClient\RequestOptions;
use Xentral\Components\Logger\LoggerAwareTrait;
use Xentral\Modules\GoogleApi\Exception\GoogleAccountNotFoundException;
use Xentral\Modules\GoogleApi\Exception\NoAccessTokenException;
use Xentral\Modules\GoogleApi\Exception\NoRefreshTokenException;
use Xentral\Modules\GoogleApi\Service\GoogleAccountGateway;
use Xentral\Modules\GoogleApi\Service\GoogleAuthorizationService;
final class GoogleApiClientFactory
{
use LoggerAwareTrait;
/** @var GoogleAccountGateway $gateway */
private $gateway;
/** @var GoogleAuthorizationService $authorizer */
private $auth;
/** @var HttpClientFactory $clientFactory */
private $clientFactory;
/**
* @param GoogleAccountGateway $gateway
* @param GoogleAuthorizationService $auth
* @param HttpClientFactory $clientFactory
*/
public function __construct(
GoogleAccountGateway $gateway,
GoogleAuthorizationService $auth,
HttpClientFactory $clientFactory
)
{
$this->gateway = $gateway;
$this->auth = $auth;
$this->clientFactory = $clientFactory;
}
/**
* @param int $userId
*
* @throws GoogleAccountNotFoundException
* @throws NoRefreshTokenException
*
* @return GoogleApiClient
*/
public function createClient(int $userId): GoogleApiClient
{
$account = $this->gateway->getAccountByUser($userId);
try{
$token = $this->gateway->getAccessToken($account->getId());
} catch (NoAccessTokenException $e) {
$token = null;
}
if ($token === null || $token->getTimeToLive() < 10) {
$token = $this->auth->refreshAccessToken($account);
}
$options = new RequestOptions();
$options->setHeader(
'Authorization',
sprintf('Bearer %s', $token->getToken())
);
$options->setHeader('Accept', 'application/json');
$httpClient = $this->clientFactory->createClient($options);
$client = new GoogleApiClient($httpClient, $account);
$client->setLogger($this->logger);
return $client;
}
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Xentral\Modules\GoogleApi\Client;
use Xentral\Modules\GoogleApi\Data\GoogleAccountData;
interface GoolgeApiClientInterface
{
/**
* @return GoogleAccountData
*/
public function getAccount(): GoogleAccountData;
/**
* @param string $method
* @param string $uri
* @param array|null $data
* @param array|null $headers
*
* @return array
*/
public function sendRequest(string $method, string $uri, array $data = null, array $headers = []): array;
}