Passed
Push — master ( 5267a9...cef136 )
by mingyoung
03:08
created

AuthorizerToken::getAppId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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
 * AuthorizerToken.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 AuthorizerToken.
36
 *
37
 * AuthorizerToken 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 AuthorizerToken extends BaseAccessToken
45
{
46
    /**
47
     * Handles authorization.
48
     *
49
     * @var Authorization
50
     */
51
    protected $authorization;
52
53
    /**
54
     * AuthorizerAccessToken constructor.
55
     *
56
     * @param string        $appId
57
     * @param Authorization $authorization
58
     */
59 3
    public function __construct($appId, Authorization $authorization)
60
    {
61 3
        parent::__construct($appId, null);
62
63 3
        $this->authorization = $authorization;
64 3
    }
65
66
    /**
67
     * Get token from WeChat API.
68
     *
69
     * @param bool $forceRefresh
70
     *
71
     * @return string
72
     */
73 3
    public function getToken($forceRefresh = false)
74
    {
75 3
        $cached = $this->authorization->getAuthorizerAccessToken();
76
77 3
        if ($forceRefresh || empty($cached)) {
78 2
            return $this->authorization->handleAuthorizerAccessToken();
79
        }
80
81 1
        return $cached;
82
    }
83
84
    /**
85
     * Get AppId for Authorizer.
86
     *
87
     * @return string
88
     */
89
    public function getAppId()
90
    {
91
        return $this->authorization->getAuthorizerAppId();
92
    }
93
}
94