Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*
3
 * Copyright 2010 Google Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
namespace Google\Auth;
19
 
20
use Psr\Cache\CacheItemPoolInterface;
21
 
22
/**
23
 * A class to implement caching for any object implementing
24
 * FetchAuthTokenInterface
25
 */
26
class FetchAuthTokenCache implements FetchAuthTokenInterface
27
{
28
    use CacheTrait;
29
 
30
    /**
31
     * @var FetchAuthTokenInterface
32
     */
33
    private $fetcher;
34
 
35
    /**
36
     * @var array
37
     */
38
    private $cacheConfig;
39
 
40
    /**
41
     * @var CacheItemPoolInterface
42
     */
43
    private $cache;
44
 
45
    public function __construct(
46
        FetchAuthTokenInterface $fetcher,
47
        array $cacheConfig = null,
48
        CacheItemPoolInterface $cache
49
    ) {
50
        $this->fetcher = $fetcher;
51
        $this->cache = $cache;
52
        $this->cacheConfig = array_merge([
53
            'lifetime' => 1500,
54
            'prefix' => '',
55
        ], (array) $cacheConfig);
56
    }
57
 
58
    /**
59
     * Implements FetchAuthTokenInterface#fetchAuthToken.
60
     *
61
     * Checks the cache for a valid auth token and fetches the auth tokens
62
     * from the supplied fetcher.
63
     *
64
     * @param callable $httpHandler callback which delivers psr7 request
65
     *
66
     * @return array the response
67
     *
68
     * @throws \Exception
69
     */
70
    public function fetchAuthToken(callable $httpHandler = null)
71
    {
72
        // Use the cached value if its available.
73
        //
74
        // TODO: correct caching; update the call to setCachedValue to set the expiry
75
        // to the value returned with the auth token.
76
        //
77
        // TODO: correct caching; enable the cache to be cleared.
78
        $cacheKey = $this->fetcher->getCacheKey();
79
        $cached = $this->getCachedValue($cacheKey);
80
        if (!empty($cached)) {
81
            return ['access_token' => $cached];
82
        }
83
 
84
        $auth_token = $this->fetcher->fetchAuthToken($httpHandler);
85
 
86
        if (isset($auth_token['access_token'])) {
87
            $this->setCachedValue($cacheKey, $auth_token['access_token']);
88
        }
89
 
90
        return $auth_token;
91
    }
92
 
93
    /**
94
     * @return string
95
     */
96
    public function getCacheKey()
97
    {
98
        return $this->getFullCacheKey($this->fetcher->getCacheKey());
99
    }
100
 
101
    /**
102
     * @return array|null
103
     */
104
    public function getLastReceivedToken()
105
    {
106
        return $this->fetcher->getLastReceivedToken();
107
    }
108
}