diff --git a/src/AccessTokenCacheHandler.php b/src/AccessTokenCacheHandler.php index 531187a..dadb0a2 100644 --- a/src/AccessTokenCacheHandler.php +++ b/src/AccessTokenCacheHandler.php @@ -10,6 +10,8 @@ class AccessTokenCacheHandler { const CACHE_KEY_PREFIX = 'oauth2-token-'; + private $cache; + public function __construct(CacheItemPoolInterface $cache) { $this->cache = $cache; diff --git a/src/RetryOnAuthorizationError.php b/src/RetryOnAuthorizationError.php index 4adadc5..86b23d8 100644 --- a/src/RetryOnAuthorizationError.php +++ b/src/RetryOnAuthorizationError.php @@ -25,11 +25,15 @@ public function __invoke( ResponseInterface $response = null, \Exception $exception = null ): bool { - $statusCode = $response->getStatusCode(); - if ($retries < 1 && $statusCode === 401) { + if ($this->isUnauthorizedResponse($retries, $response)) { $this->cacheHandler->deleteItemByProvider($this->provider, $this->config); return true; } return false; } + + private function isUnauthorizedResponse(int $retries, ResponseInterface $response = null) + { + return !empty($response) && $retries < 1 && $response->getStatusCode() === 401; + } } diff --git a/tests/RetryOnAuthorizationErrorTest.php b/tests/RetryOnAuthorizationErrorTest.php index 2a4c4e5..7f603c5 100644 --- a/tests/RetryOnAuthorizationErrorTest.php +++ b/tests/RetryOnAuthorizationErrorTest.php @@ -29,7 +29,7 @@ public function testRetryOnceOn401Response() $mockProvider = $this->createMock(\League\OAuth2\Client\Provider\AbstractProvider::class); $mockCacheHandler = $this->createMock(\Softonic\OAuth2\Guzzle\Middleware\AccessTokenCacheHandler::class); - $mockResponse->expects($this->exactly(2)) + $mockResponse->expects($this->exactly(1)) ->method('getStatusCode') ->willReturn(401);