push() публичный статический Метод

Persists the data into a cache file provided in $CACHE_PATH
public static push ( array | null $config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn )
$config array | null
$clientId
$accessToken
$tokenCreateTime
$tokenExpiresIn
 public function testCachePush()
 {
     AuthorizationCache::push(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId', 'accessToken', 'tokenCreateTime', 'tokenExpiresIn');
     $contents = file_get_contents(AuthorizationCacheTest::CACHE_FILE);
     $tokens = json_decode($contents, true);
     $this->assertNotNull($contents);
     $this->assertEquals('clientId', $tokens['clientId']['clientId']);
     $this->assertEquals('accessToken', $tokens['clientId']['accessTokenEncrypted']);
     $this->assertEquals('tokenCreateTime', $tokens['clientId']['tokenCreateTime']);
     $this->assertEquals('tokenExpiresIn', $tokens['clientId']['tokenExpiresIn']);
 }
 public function testGetAccessTokenUnit()
 {
     $config = array('mode' => 'sandbox', 'cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE);
     $cred = new OAuthTokenCredential('clientId', 'clientSecret');
     //{"clientId":{"clientId":"clientId","accessToken":"accessToken","tokenCreateTime":1421204091,"tokenExpiresIn":288000000}}
     AuthorizationCache::push($config, 'clientId', $cred->encrypt('accessToken'), 1421204091, 288000000);
     $apiContext = new ApiContext($cred);
     $apiContext->setConfig($config);
     $this->assertEquals('clientId', $cred->getClientId());
     $this->assertEquals('clientSecret', $cred->getClientSecret());
     $result = $cred->getAccessToken($config);
     $this->assertNotNull($result);
 }
 /**
  * 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;
 }
Пример #4
0
 /**
  * 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;
 }