Completed
Push — master ( 931b65...d2ebaa )
by Carlos
03:47
created

AuthorizerAccessToken::refreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.4285
ccs 8
cts 8
cp 1
crap 1
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
 * AuthorizerAccessToken.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    lixiao <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\OpenPlatform;
28
29
// Don't change the alias name please. I met the issue "name already in use"
30
// when used in Laravel project, not sure what is causing it, this is quick
31
// solution.
32
use EasyWeChat\Core\AccessToken as BaseAccessToken;
33
34
/**
35
 * Class AuthorizerAccessToken.
36
 *
37
 * AuthorizerAccessToken is responsible for the access token of the authorizer,
38
 * the complexity is that this access token also requires the refresh token
39
 * of the authorizer which is acquired by the open platform authorization
40
 * process.
41
 *
42
 * This completely overrides the original AccessToken.
43
 */
44
class AuthorizerAccessToken extends BaseAccessToken
45
{
46
    /**
47
     * @var \EasyWeChat\OpenPlatform\Authorization
48
     */
49
    protected $authorization;
50
51
    /**
52
     * AuthorizerAccessToken constructor.
53
     *
54
     * @param string                                 $appId
55
     * @param \EasyWeChat\OpenPlatform\Authorization $authorization
56
     */
57 4
    public function __construct($appId, Authorization $authorization)
58
    {
59 4
        parent::__construct($appId, null);
60
61 4
        $this->authorization = $authorization;
62 4
    }
63
64
    /**
65
     * Get token from WeChat API.
66
     *
67
     * @param bool $forceRefresh
68
     *
69
     * @return string
70
     */
71 3
    public function getToken($forceRefresh = false)
72
    {
73 3
        $cached = $this->authorization->getAuthorizerAccessToken();
74
75 3
        if ($forceRefresh || empty($cached)) {
76 2
            return $this->refreshToken();
77
        }
78
79 1
        return $cached;
80
    }
81
82
    /**
83
     * Refresh authorizer access token.
84
     *
85
     * @return string
86
     */
87 2
    protected function refreshToken()
88
    {
89 2
        $token = $this->authorization->getApi()
90 2
            ->getAuthorizerToken(
91 2
                $this->authorization->getAuthorizerAppId(),
92 2
                $this->authorization->getAuthorizerRefreshToken()
93 2
            );
94
95 2
        $this->authorization->setAuthorizerAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500);
96
97 2
        return $token['authorizer_access_token'];
98
    }
99
100
    /**
101
     * Get AppId for Authorizer.
102
     *
103
     * @return string
104
     */
105 1
    public function getAppId()
106
    {
107 1
        return $this->authorization->getAuthorizerAppId();
108
    }
109
}
110