Test Setup Failed
Pull Request — master (#606)
by
unknown
02:50
created

AuthorizerToken::getToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 10
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
 * 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
use EasyWeChat\Core\AccessToken;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EasyWeChat\OpenPlatform\AccessToken.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
30
31
/**
32
 * Class AuthorizerToken
33
 *
34
 * AuthorizerToken is responsible for the access token of the authorizer,
35
 * the complexity is that this access token also requires the refresh token
36
 * of the authorizer which is acquired by the open platform authorization
37
 * process.
38
 *
39
 * This completely overrides the original AccessToken.
40
 *
41
 * @package EasyWeChat\OpenPlatform
42
 */
43
class AuthorizerToken extends AccessToken
44
{
45
    /**
46
     * Handles authorization.
47
     *
48
     * @var Authorization
49
     */
50
    protected $authorization;
51
52
    /**
53
     * AuthorizerAccessToken constructor.
54
     *
55
     * @param string $appId
56
     * @param Authorization $authorization
57
     */
58
    public function __construct($appId, Authorization $authorization)
59
    {
60
        parent::__construct($appId, null);
61
62
        $this->authorization = $authorization;
63
    }
64
65
    /**
66
     * Get token from WeChat API.
67
     *
68
     * @param bool $forceRefresh
69
     *
70
     * @return string
71
     */
72
    public function getToken($forceRefresh = false)
73
    {
74
        $cached = $this->authorization->getAuthorizerAccessToken();
75
76
        if ($forceRefresh || empty($cached)) {
77
            return $this->authorization->handleAuthorizerAccessToken();
78
        }
79
80
        return $cached;
81
    }
82
}
83