From 9c8e482e4f4e9c6c3f5edb648ab169d889e09abe Mon Sep 17 00:00:00 2001 From: egrigorev Date: Thu, 25 Apr 2024 21:55:45 +0300 Subject: [PATCH 1/3] add refresh access token callback --- src/AmoCRM/Client/AmoCRMApiClient.php | 26 +++++++++++++++++++++++++- src/AmoCRM/Client/AmoCRMApiRequest.php | 20 +++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/AmoCRM/Client/AmoCRMApiClient.php b/src/AmoCRM/Client/AmoCRMApiClient.php index b5eb466..a678e39 100644 --- a/src/AmoCRM/Client/AmoCRMApiClient.php +++ b/src/AmoCRM/Client/AmoCRMApiClient.php @@ -86,6 +86,11 @@ class AmoCRMApiClient */ private $userAgent; + /** + * @var callable|null + */ + private $refreshAccessTokenCallback; + /** * AmoCRMApiClient constructor. * @@ -181,6 +186,19 @@ public function onAccessTokenRefresh(callable $callback): self return $this; } + /** + * Устанавливаем callback, который будет вызван для обновления AccessToken`a библиотеки + * + * @param callable $callable + * @return $this + */ + public function onRefreshAccessTokenCallback(callable $callable): self + { + $this->refreshAccessTokenCallback = $callable; + + return $this; + } + /** * Метод строит объект для совершения запросов для сервисов сущностей * @@ -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; } /** diff --git a/src/AmoCRM/Client/AmoCRMApiRequest.php b/src/AmoCRM/Client/AmoCRMApiRequest.php index 53e5ade..22996f9 100644 --- a/src/AmoCRM/Client/AmoCRMApiRequest.php +++ b/src/AmoCRM/Client/AmoCRMApiRequest.php @@ -107,6 +107,11 @@ class AmoCRMApiRequest /** @var string|null */ private $userAgent = null; + /** + * @var callable + */ + private $refreshAccessTokenCallback; + /** * AmoCRMApiRequest constructor. * @@ -126,6 +131,19 @@ public function __construct( $this->httpClient = $oAuthClient->getHttpClient(); $this->contextUserId = $contextUserId; $this->userAgent = $userAgent; + $this->refreshAccessTokenCallback = function () { + return $this->oAuthClient->getAccessTokenByRefreshToken($this->accessToken); + }; + } + + public function getRefreshAccessTokenCallback(): callable + { + return $this->refreshAccessTokenCallback; + } + + public function setRefreshAccessTokenCallback(callable $callback): void + { + $this->refreshAccessTokenCallback = $callback; } /** @@ -138,7 +156,7 @@ private function refreshAccessToken(): void throw new AmoCRMoAuthApiException('Can not update LongLivedAccessToken'); } - $newAccessToken = $this->oAuthClient->getAccessTokenByRefreshToken($this->accessToken); + $newAccessToken = $this->getRefreshAccessTokenCallback(); $this->accessToken = $newAccessToken; } From 7d14e4e30adca23f51f25adb2e593e2034189de3 Mon Sep 17 00:00:00 2001 From: egrigorev Date: Thu, 25 Apr 2024 22:15:53 +0300 Subject: [PATCH 2/3] fix --- src/AmoCRM/Client/AmoCRMApiRequest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/AmoCRM/Client/AmoCRMApiRequest.php b/src/AmoCRM/Client/AmoCRMApiRequest.php index 22996f9..7c6d037 100644 --- a/src/AmoCRM/Client/AmoCRMApiRequest.php +++ b/src/AmoCRM/Client/AmoCRMApiRequest.php @@ -136,11 +136,6 @@ public function __construct( }; } - public function getRefreshAccessTokenCallback(): callable - { - return $this->refreshAccessTokenCallback; - } - public function setRefreshAccessTokenCallback(callable $callback): void { $this->refreshAccessTokenCallback = $callback; @@ -156,7 +151,7 @@ private function refreshAccessToken(): void throw new AmoCRMoAuthApiException('Can not update LongLivedAccessToken'); } - $newAccessToken = $this->getRefreshAccessTokenCallback(); + $newAccessToken = ($this->refreshAccessTokenCallback)(); $this->accessToken = $newAccessToken; } From d2c012e2b22106bdccf100d8ff5b4187fb2352a1 Mon Sep 17 00:00:00 2001 From: egrigorev Date: Fri, 26 Apr 2024 16:48:44 +0300 Subject: [PATCH 3/3] add test and upgrade library version --- src/AmoCRM/Client/AmoCRMApiClient.php | 2 +- src/AmoCRM/Client/AmoCRMApiRequest.php | 2 +- tests/Cases/AmoCRM/Client/AmoCRMApiClientTest.php | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/AmoCRM/Client/AmoCRMApiClient.php b/src/AmoCRM/Client/AmoCRMApiClient.php index a678e39..b32483a 100644 --- a/src/AmoCRM/Client/AmoCRMApiClient.php +++ b/src/AmoCRM/Client/AmoCRMApiClient.php @@ -192,7 +192,7 @@ public function onAccessTokenRefresh(callable $callback): self * @param callable $callable * @return $this */ - public function onRefreshAccessTokenCallback(callable $callable): self + public function setRefreshAccessTokenCallback(callable $callable): self { $this->refreshAccessTokenCallback = $callable; diff --git a/src/AmoCRM/Client/AmoCRMApiRequest.php b/src/AmoCRM/Client/AmoCRMApiRequest.php index 6507a16..cf9a9be 100644 --- a/src/AmoCRM/Client/AmoCRMApiRequest.php +++ b/src/AmoCRM/Client/AmoCRMApiRequest.php @@ -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 = [ diff --git a/tests/Cases/AmoCRM/Client/AmoCRMApiClientTest.php b/tests/Cases/AmoCRM/Client/AmoCRMApiClientTest.php index e67ef73..1d9fe4a 100644 --- a/tests/Cases/AmoCRM/Client/AmoCRMApiClientTest.php +++ b/tests/Cases/AmoCRM/Client/AmoCRMApiClientTest.php @@ -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(