Completed
Pull Request — master (#474)
by
unknown
03:52
created

AccessToken::getCacheKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.4285
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * AccessToken.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Core;
22
23
use Doctrine\Common\Cache\Cache;
24
use Doctrine\Common\Cache\FilesystemCache;
25
use EasyWeChat\Core\Exceptions\HttpException;
26
27
/**
28
 * Class AccessToken.
29
 */
30
class AccessToken
31
{
32
    /**
33
     * App ID.
34
     *
35
     * @var string
36
     */
37
    protected $appId;
38
39
    /**
40
     * App secret.
41
     *
42
     * @var string
43
     */
44
    protected $secret;
45
46
    /**
47
     * Cache.
48
     *
49
     * @var Cache
50
     */
51
    protected $cache;
52
53
    /**
54
     * Cache Key.
55
     *
56
     * @var cacheKey
57
     */
58
    protected $cacheKey;
59
60
    /**
61
     * Http instance.
62
     *
63
     * @var Http
64
     */
65
    protected $http;
66
67
    /**
68
     * Query name.
69
     *
70
     * @var string
71
     */
72
    protected $queryName = 'access_token';
73
74
    /**
75
     * Cache key prefix.
76
     *
77
     * @var string
78
     */
79
    protected $prefix = 'easywechat.common.access_token.';
80
81
    // API
82
    const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/token';
83
84
    /**
85
     * Constructor.
86
     *
87
     * @param string                       $appId
88
     * @param string                       $secret
89
     * @param \Doctrine\Common\Cache\Cache $cache
90
     */
91 43
    public function __construct($appId, $secret, Cache $cache = null)
92
    {
93 43
        $this->appId = $appId;
94 43
        $this->secret = $secret;
95 43
        $this->cache = $cache;
96 43
    }
97
98
    /**
99
     * Get token from WeChat API.
100
     *
101
     * @param bool $forceRefresh
102
     *
103
     * @return string
104
     */
105 3
    public function getToken($forceRefresh = false)
106
    {
107 3
        $cacheKey = $this->getCacheKey();
108 3
        $cached = $this->getCache()->fetch($cacheKey);
109
110 3
        if ($forceRefresh || empty($cached)) {
111 2
            $token = $this->getTokenFromServer();
112
113
            // XXX: T_T... 7200 - 1500
114 2
            $this->getCache()->save($cacheKey, $token['access_token'], $token['expires_in'] - 1500);
115
116 2
            return $token['access_token'];
117
        }
118
119 2
        return $cached;
120
    }
121
122
    /**
123
     * Return the app id.
124
     *
125
     * @return string
126
     */
127 8
    public function getAppId()
128
    {
129 8
        return $this->appId;
130
    }
131
132
    /**
133
     * Return the secret.
134
     *
135
     * @return string
136
     */
137 1
    public function getSecret()
138
    {
139 1
        return $this->secret;
140
    }
141
142
    /**
143
     * Set cache instance.
144
     *
145
     * @param \Doctrine\Common\Cache\Cache $cache
146
     *
147
     * @return AccessToken
148
     */
149 1
    public function setCache(Cache $cache)
150
    {
151 1
        $this->cache = $cache;
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * Return the cache manager.
158
     *
159
     * @return \Doctrine\Common\Cache\Cache
160
     */
161 3
    public function getCache()
162
    {
163 3
        return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
164
    }
165
166
    /**
167
     * Set the query name.
168
     *
169
     * @param string $queryName
170
     *
171
     * @return $this
172
     */
173 1
    public function setQueryName($queryName)
174
    {
175 1
        $this->queryName = $queryName;
176
177 1
        return $this;
178
    }
179
180
    /**
181
     * Return the query name.
182
     *
183
     * @return string
184
     */
185 1
    public function getQueryName()
186
    {
187 1
        return $this->queryName;
188
    }
189
190
    /**
191
     * Return the API request queries.
192
     *
193
     * @return array
194
     */
195 1
    public function getQueryFields()
196
    {
197 1
        return [$this->queryName => $this->getToken()];
198
    }
199
200
    /**
201
     * Get the access token from WeChat server.
202
     *
203
     * @throws \EasyWeChat\Core\Exceptions\HttpException
204
     *
205
     * @return array|bool
206
     */
207 2
    public function getTokenFromServer()
208
    {
209
        $params = [
210 2
            'appid' => $this->appId,
211 2
            'secret' => $this->secret,
212 2
            'grant_type' => 'client_credential',
213 2
        ];
214
215 2
        $http = $this->getHttp();
216
217 2
        $token = $http->parseJSON($http->get(self::API_TOKEN_GET, $params));
218
219 2
        if (empty($token['access_token'])) {
220 1
            throw new HttpException('Request AccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE));
221
        }
222
223 2
        return $token;
224
    }
225
226
    /**
227
     * Return the http instance.
228
     *
229
     * @return \EasyWeChat\Core\Http
230
     */
231 2
    public function getHttp()
232
    {
233 2
        return $this->http ?: $this->http = new Http();
234
    }
235
236
    /**
237
     * Set the http instance.
238
     *
239
     * @param \EasyWeChat\Core\Http $http
240
     *
241
     * @return $this
242
     */
243 2
    public function setHttp(Http $http)
244
    {
245 2
        $this->http = $http;
246
247 2
        return $this;
248
    }
249
250
    /**
251
     * Set the access token prefix.
252
     *
253
     * @param string $prefix
254
     *
255
     * @return $this
256
     */
257
    public function setPrefix($prefix)
258
    {
259
        $this->prefix = $prefix;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Set access token cache key.
266
     *
267
     * @param string $cacheKey
268
     *
269
     * @return $this
270
     */
271
    public function setCacheKey($cacheKey)
272
    {
273
        $this->cacheKey = $cacheKey;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheKey of type string is incompatible with the declared type object<EasyWeChat\Core\cacheKey> of property $cacheKey.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
274
275
        return $this;
276
    }
277
278
    /**
279
     * Get access token cache key.
280
     *
281
     * @return string $this->cacheKey
282
     */
283 3
    public function getCacheKey()
284
    {
285 3
        if (is_null($this->cacheKey)) {
286 3
            return $this->prefix.$this->appId;
287
        }
288
289
        return $this->cacheKey;
290
    }
291
}
292