Completed
Pull Request — master (#659)
by mingyoung
03:45
created

AuthorizerAccessToken::renewAccessToken()   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
nc 1
nop 0
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 authorizer process.
40
 *
41
 * This completely overrides the original AccessToken.
42
 */
43
class AuthorizerAccessToken extends BaseAccessToken
44
{
45
    /**
46
     * @var \EasyWeChat\OpenPlatform\Authorizer
47
     */
48
    protected $authorizer;
49
50
    /**
51
     * AuthorizerAccessToken constructor.
52
     *
53
     * @param string                              $appId
54
     * @param \EasyWeChat\OpenPlatform\Authorizer $authorizer
55
     */
56 4
    public function __construct($appId, Authorizer $authorizer)
57
    {
58 4
        parent::__construct($appId, null);
59
60 4
        $this->authorizer = $authorizer;
61 4
    }
62
63
    /**
64
     * Get token from WeChat API.
65
     *
66
     * @param bool $forceRefresh
67
     *
68
     * @return string
69
     */
70 3
    public function getToken($forceRefresh = false)
71
    {
72 3
        $cached = $this->authorizer->getAccessToken();
73
74 3
        if ($forceRefresh || empty($cached)) {
75 2
            return $this->renewAccessToken();
76
        }
77
78 1
        return $cached;
79
    }
80
81
    /**
82
     * Refresh authorizer access token.
83
     *
84
     * @return string
85
     */
86 2
    protected function renewAccessToken()
87
    {
88 2
        $token = $this->authorizer->getApi()
89 2
            ->getAuthorizerToken(
90 2
                $this->authorizer->getAppId(),
91 2
                $this->authorizer->getRefreshToken()
92 2
            );
93
94 2
        $this->authorizer->setAccessToken($token['authorizer_access_token'], $token['expires_in'] - 1500);
95
96 2
        return $token['authorizer_access_token'];
97
    }
98
99
    /**
100
     * Return the AuthorizerAppId.
101
     *
102
     * @return string
103
     */
104 1
    public function getAppId()
105
    {
106 1
        return $this->authorizer->getAppId();
107
    }
108
}
109