Skip to content

Commit

Permalink
Merge branch 'feature/AC-862' into 'master'
Browse files Browse the repository at this point in the history
AC-862 Добавил метод, который позволяет устанавливать callback для получение нового access token`a

See merge request amocrm/amocrm-api-php!48
  • Loading branch information
Nikita committed Apr 26, 2024
2 parents b7a3ca0 + d2c012e commit 1f3ae41
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/AmoCRM/Client/AmoCRMApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class AmoCRMApiClient
*/
private $userAgent;

/**
* @var callable|null
*/
private $refreshAccessTokenCallback;

/**
* AmoCRMApiClient constructor.
*
Expand Down Expand Up @@ -181,6 +186,19 @@ public function onAccessTokenRefresh(callable $callback): self
return $this;
}

/**
* Устанавливаем callback, который будет вызван для обновления AccessToken`a библиотеки
*
* @param callable $callable
* @return $this
*/
public function setRefreshAccessTokenCallback(callable $callable): self
{
$this->refreshAccessTokenCallback = $callable;

return $this;
}

/**
* Метод строит объект для совершения запросов для сервисов сущностей
*
Expand All @@ -206,12 +224,18 @@ function (AccessToken $accessToken, string $baseAccountDomain) use ($oAuthClient
}
);

return new AmoCRMApiRequest(
$request = new AmoCRMApiRequest(
$this->getAccessToken(),
$oAuthClient,
$this->getContextUserId(),
$this->getUserAgent()
);

if ($this->refreshAccessTokenCallback !== null) {
$request->setRefreshAccessTokenCallback($this->refreshAccessTokenCallback);
}

return $request;
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/AmoCRM/Client/AmoCRMApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AmoCRMApiRequest
public const CONNECT_TIMEOUT = 5;
public const REQUEST_TIMEOUT = 20;
//TODO Do not forget to change this on each release
public const LIBRARY_VERSION = '1.7.2';
public const LIBRARY_VERSION = '1.7.3';
public const USER_AGENT = 'amoCRM-API-Library/' . self::LIBRARY_VERSION;

public const SUCCESS_STATUSES = [
Expand Down Expand Up @@ -107,6 +107,11 @@ class AmoCRMApiRequest
/** @var string|null */
private $userAgent = null;

/**
* @var callable
*/
private $refreshAccessTokenCallback;

/**
* AmoCRMApiRequest constructor.
*
Expand All @@ -126,6 +131,14 @@ public function __construct(
$this->httpClient = $oAuthClient->getHttpClient();
$this->contextUserId = $contextUserId;
$this->userAgent = $userAgent;
$this->refreshAccessTokenCallback = function () {
return $this->oAuthClient->getAccessTokenByRefreshToken($this->accessToken);
};
}

public function setRefreshAccessTokenCallback(callable $callback): void
{
$this->refreshAccessTokenCallback = $callback;
}

/**
Expand All @@ -138,7 +151,7 @@ private function refreshAccessToken(): void
throw new AmoCRMoAuthApiException('Can not update LongLivedAccessToken');
}

$newAccessToken = $this->oAuthClient->getAccessTokenByRefreshToken($this->accessToken);
$newAccessToken = ($this->refreshAccessTokenCallback)();
$this->accessToken = $newAccessToken;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Cases/AmoCRM/Client/AmoCRMApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ public function testOnAccessTokenRefresh()
$this->assertIsCallable($callback);
}

public function testSetOnRefreshAccessTokenCallback()
{
$this->assertInstanceOf(
AmoCRMApiClient::class,
$this->apiClient->setRefreshAccessTokenCallback(function () {
})
);
$callback = $this->_getInnerPropertyValueByReflection('refreshAccessTokenCallback');
$this->assertIsCallable($callback);
}

public function testSetAccountBaseDomain()
{
$this->assertInstanceOf(
Expand Down

0 comments on commit 1f3ae41

Please sign in to comment.