PayPal\Cache\AuthorizationCache::push PHP Method

push() public static method

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 static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn)
    {
        // Return if not enabled
        if (!self::isEnabled($config)) {
            return;
        }
        $cachePath = self::cachePath($config);
        if (!is_dir(dirname($cachePath))) {
            if (mkdir(dirname($cachePath), 0755, true) == false) {
                throw new \Exception("Failed to create directory at {$cachePath}");
            }
        }
        // Reads all the existing persisted data
        $tokens = self::pull();
        $tokens = $tokens ? $tokens : array();
        if (is_array($tokens)) {
            $tokens[$clientId] = array('clientId' => $clientId, 'accessTokenEncrypted' => $accessToken, 'tokenCreateTime' => $tokenCreateTime, 'tokenExpiresIn' => $tokenExpiresIn);
        }
        if (!file_put_contents($cachePath, json_encode($tokens))) {
            throw new \Exception("Failed to write cache");
        }
    }

Usage Example

 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']);
 }
All Usage Examples Of PayPal\Cache\AuthorizationCache::push