Skip to content

Commit

Permalink
Merge pull request #334 from mesilov/332-undefined-index
Browse files Browse the repository at this point in the history
fix undefined index error
  • Loading branch information
mesilov authored Feb 17, 2023
2 parents 75096b2 + 5afb67b commit a5f67d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ jobs:
php-version:
- "8.1"
- "8.2"
dependencies:
- "lowest"
- "highest"
dependencies: [ highest ]

steps:
- name: "Checkout"
Expand Down
46 changes: 27 additions & 19 deletions src/Core/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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,
];
}

Expand All @@ -74,6 +76,7 @@ public function getCredentials(): Credentials\Credentials
* @throws InvalidArgumentException
* @throws TransportExceptionInterface
* @throws \JsonException
* @throws TransportException
*/
public function getNewAccessToken(): RenewedAccessToken
{
Expand All @@ -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(),
]
Expand All @@ -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<mixed> $parameters
*
* @return ResponseInterface
Expand All @@ -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,
]
);
Expand All @@ -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,
];
Expand All @@ -153,7 +161,7 @@ public function getResponse(string $apiMethod, array $parameters = []): Response
$this->logger->info(
'getResponse.end',
[
'apiMethod' => $apiMethod,
'apiMethod' => $apiMethod,
'responseInfo' => $response->getInfo(),
]
);
Expand Down

0 comments on commit a5f67d0

Please sign in to comment.