Passed
Push — master ( e38f5c...931b65 )
by Carlos
06:23 queued 02:38
created

AuthorizerAccessToken   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 48
ccs 11
cts 11
cp 1
rs 10
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getToken() 0 10 3
A getAppId() 0 4 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->authorization->handleAuthorizerAccessToken();
77
        }
78
79 1
        return $cached;
80
    }
81
82
    /**
83
     * Get AppId for Authorizer.
84
     *
85
     * @return string
86
     */
87 1
    public function getAppId()
88
    {
89 1
        return $this->authorization->getAuthorizerAppId();
90
    }
91
}
92