/** * @depends testCachePush */ public function testCachePull() { $result = AuthorizationCache::pull(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId'); $this->assertNotNull($result); $this->assertTrue(is_array($result)); $this->assertEquals('clientId', $result['clientId']); $this->assertEquals('accessToken', $result['accessTokenEncrypted']); $this->assertEquals('tokenCreateTime', $result['tokenCreateTime']); $this->assertEquals('tokenExpiresIn', $result['tokenExpiresIn']); unlink(AuthorizationCacheTest::CACHE_FILE); }
/** * Get AccessToken * * @param $config * * @return null|string */ public function getAccessToken($config) { // Check if we already have accessToken in Cache if ($this->accessToken && time() - $this->tokenCreateTime < $this->tokenExpiresIn - self::$expiryBufferTime) { return $this->accessToken; } // Check for persisted data first $token = AuthorizationCache::pull($config, $this->clientId); if ($token) { // We found it // This code block is for backward compatibility only. if (array_key_exists('accessToken', $token)) { $this->accessToken = $token['accessToken']; } $this->tokenCreateTime = $token['tokenCreateTime']; $this->tokenExpiresIn = $token['tokenExpiresIn']; // Case where we have an old unencrypted cache file if (!array_key_exists('accessTokenEncrypted', $token)) { AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn); } else { $this->accessToken = $this->decrypt($token['accessTokenEncrypted']); } } // Check if Access Token is not null and has not expired. // The API returns expiry time as a relative time unit // We use a buffer time when checking for token expiry to account // for API call delays and any delay between the time the token is // retrieved and subsequently used if ($this->accessToken != null && time() - $this->tokenCreateTime > $this->tokenExpiresIn - self::$expiryBufferTime) { $this->accessToken = null; } // If accessToken is Null, obtain a new token if ($this->accessToken == null) { // Get a new one by making calls to API $this->updateAccessToken($config); AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn); } return $this->accessToken; }
/** * Get AccessToken * * @param $config * * @return null|string */ public function getAccessToken($config) { // Check for persisted data first $token = AuthorizationCache::pull($config, $this->clientId); if ($token) { // We found it $this->accessToken = $token['accessToken']; $this->tokenCreateTime = $token['tokenCreateTime']; $this->tokenExpiresIn = $token['tokenExpiresIn']; } // Check if Access Token is not null and has not expired. // The API returns expiry time as a relative time unit // We use a buffer time when checking for token expiry to account // for API call delays and any delay between the time the token is // retrieved and subsequently used if ($this->accessToken != null && time() - $this->tokenCreateTime > $this->tokenExpiresIn - self::$expiryBufferTime) { $this->accessToken = null; } // If accessToken is Null, obtain a new token if ($this->accessToken == null) { // Get a new one by making calls to API $this->updateAccessToken($config); AuthorizationCache::push($config, $this->clientId, $this->accessToken, $this->tokenCreateTime, $this->tokenExpiresIn); } return $this->accessToken; }