From fc98eb51a77d33c6f8257417e86fcb6ef4fd4806 Mon Sep 17 00:00:00 2001 From: mesilov Date: Fri, 17 Feb 2023 17:27:24 +0400 Subject: [PATCH 1/2] fix undefined index error Signed-off-by: mesilov --- src/Core/ApiClient.php | 46 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Core/ApiClient.php b/src/Core/ApiClient.php index 46ab0497..ac7387e0 100644 --- a/src/Core/ApiClient.php +++ b/src/Core/ApiClient.php @@ -6,7 +6,9 @@ use Bitrix24\SDK\Core\Contracts\ApiClientInterface; use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; +use Bitrix24\SDK\Core\Exceptions\TransportException; use Bitrix24\SDK\Core\Response\DTO\RenewedAccessToken; +use Fig\Http\Message\StatusCodeInterface; use Psr\Log\LoggerInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -31,8 +33,8 @@ class ApiClient implements ApiClientInterface * ApiClient constructor. * * @param Credentials\Credentials $credentials - * @param HttpClientInterface $client - * @param LoggerInterface $logger + * @param HttpClientInterface $client + * @param LoggerInterface $logger */ public function __construct(Credentials\Credentials $credentials, HttpClientInterface $client, LoggerInterface $logger) { @@ -53,11 +55,11 @@ public function __construct(Credentials\Credentials $credentials, HttpClientInte protected function getDefaultHeaders(): array { return [ - 'Accept' => 'application/json', - 'Accept-Charset' => 'utf-8', - 'User-Agent' => sprintf('%s-v-%s-php-%s', self::SDK_USER_AGENT, self::SDK_VERSION, PHP_VERSION), + 'Accept' => 'application/json', + 'Accept-Charset' => 'utf-8', + 'User-Agent' => sprintf('%s-v-%s-php-%s', self::SDK_USER_AGENT, self::SDK_VERSION, PHP_VERSION), 'X-BITRIX24-PHP-SDK-PHP-VERSION' => PHP_VERSION, - 'X-BITRIX24-PHP-SDK-VERSION' => self::SDK_VERSION, + 'X-BITRIX24-PHP-SDK-VERSION' => self::SDK_VERSION, ]; } @@ -74,6 +76,7 @@ public function getCredentials(): Credentials\Credentials * @throws InvalidArgumentException * @throws TransportExceptionInterface * @throws \JsonException + * @throws TransportException */ public function getNewAccessToken(): RenewedAccessToken { @@ -91,8 +94,8 @@ public function getNewAccessToken(): RenewedAccessToken $this::BITRIX24_OAUTH_SERVER_URL, http_build_query( [ - 'grant_type' => 'refresh_token', - 'client_id' => $this->getCredentials()->getApplicationProfile()->getClientId(), + 'grant_type' => 'refresh_token', + 'client_id' => $this->getCredentials()->getApplicationProfile()->getClientId(), 'client_secret' => $this->getCredentials()->getApplicationProfile()->getClientSecret(), 'refresh_token' => $this->getCredentials()->getAccessToken()->getRefreshToken(), ] @@ -103,16 +106,21 @@ public function getNewAccessToken(): RenewedAccessToken 'headers' => $this->getDefaultHeaders(), ]; $response = $this->client->request($method, $url, $requestOptions); - $result = $response->toArray(false); - $newAccessToken = RenewedAccessToken::initFromArray($result); + $responseData = $response->toArray(false); + if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { + $newAccessToken = RenewedAccessToken::initFromArray($responseData); - $this->logger->debug('getNewAccessToken.finish'); - - return $newAccessToken; + $this->logger->debug('getNewAccessToken.finish'); + return $newAccessToken; + } + if ($response->getStatusCode() === StatusCodeInterface::STATUS_BAD_REQUEST) { + throw new TransportException(sprintf('getting new access token failure: %s', $responseData['error'])); + } + throw new TransportException('getting new access token failure with unknown http-status code %s', $response->getStatusCode()); } /** - * @param string $apiMethod + * @param string $apiMethod * @param array $parameters * * @return ResponseInterface @@ -124,8 +132,8 @@ public function getResponse(string $apiMethod, array $parameters = []): Response $this->logger->info( 'getResponse.start', [ - 'apiMethod' => $apiMethod, - 'domainUrl' => $this->credentials->getDomainUrl(), + 'apiMethod' => $apiMethod, + 'domainUrl' => $this->credentials->getDomainUrl(), 'parameters' => $parameters, ] ); @@ -143,8 +151,8 @@ public function getResponse(string $apiMethod, array $parameters = []): Response } $requestOptions = [ - 'json' => $parameters, - 'headers' => $this->getDefaultHeaders(), + 'json' => $parameters, + 'headers' => $this->getDefaultHeaders(), // disable redirects, try to catch portal change domain name event 'max_redirects' => 0, ]; @@ -153,7 +161,7 @@ public function getResponse(string $apiMethod, array $parameters = []): Response $this->logger->info( 'getResponse.end', [ - 'apiMethod' => $apiMethod, + 'apiMethod' => $apiMethod, 'responseInfo' => $response->getInfo(), ] ); From 5afb67ba4e63acc95449ecce5817ebfd13f98322 Mon Sep 17 00:00:00 2001 From: mesilov Date: Fri, 17 Feb 2023 17:31:21 +0400 Subject: [PATCH 2/2] remove phpstan check on lowest versions Signed-off-by: mesilov --- .github/workflows/phpstan.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index d2826503..068e9864 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -15,9 +15,7 @@ jobs: php-version: - "8.1" - "8.2" - dependencies: - - "lowest" - - "highest" + dependencies: [ highest ] steps: - name: "Checkout"