Test Setup Failed
Push — master ( d37a6a...512c0e )
by Carlos
03:19
created

Authorization::getAuthorizerRefreshToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Authorization.php.
4
 *
5
 * Part of Overtrue\WeChat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    lixiao <[email protected]>
11
 * @copyright 2016
12
 *
13
 * @see      https://github.com/overtrue
14
 * @see      http://overtrue.me
15
 */
16
17
namespace EasyWeChat\OpenPlatform;
18
19
use Doctrine\Common\Cache\Cache;
20
use EasyWeChat\Core\Exception;
21
use EasyWeChat\OpenPlatform\Components\Authorizer;
22
use EasyWeChat\OpenPlatform\Traits\Caches;
23
use EasyWeChat\Support\Collection;
24
25
class Authorization
26
{
27
    use Caches;
28
29
    const CACHE_KEY_ACCESS_TOKEN  = 'easywechat.open_platform.authorizer_access_token';
30
    const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token';
31
32
    /**
33
     * Authorizer API.
34
     *
35
     * @var Authorizer
36
     */
37
    private $authorizer;
38
39
    /**
40
     * Open Platform App Id, aka, Component App Id.
41
     *
42
     * @var string
43
     */
44
    private $appId;
45
46
    /**
47
     * Authorizer App Id.
48
     *
49
     * @var string
50
     */
51
    private $authorizerAppId;
52
53
    /**
54
     * Auth code.
55
     *
56
     * @var string
57
     */
58
    private $authCode;
59
60
    public function __construct(Authorizer $authorizer, $appId,
61
                                Cache $cache = null)
62
    {
63
        $this->authorizer = $authorizer;
64
        $this->appId = $appId;
65
        $this->setCache($cache);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by parameter $cache on line 61 can be null; however, EasyWeChat\OpenPlatform\Traits\Caches::setCache() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
66
    }
67
68
    /**
69
     * Sets the authorizer app id.
70
     *
71
     * @param string $authorizerAppId
72
     */
73
    public function setAuthorizerAppId($authorizerAppId)
74
    {
75
        $this->authorizerAppId = $authorizerAppId;
76
    }
77
78
    /**
79
     * Gets the authorizer app id, or throws if not found.
80
     *
81
     * @return string
82
     * @throws Exception
83
     */
84
    public function getAuthorizerAppId()
85
    {
86
        if (! $this->authorizerAppId) {
87
            throw new Exception(
88
                'Authorizer App Id is not present, you may not make the authorization yet.'
89
            );
90
        }
91
92
        return $this->authorizerAppId;
93
    }
94
95
    /**
96
     * Sets the auth code.
97
     *
98
     * @param $code
99
     */
100
    public function setAuthCode($code)
101
    {
102
        $this->authCode = $code;
103
    }
104
105
    /**
106
     * Gets the auth code.
107
     *
108
     * @return string
109
     */
110
    public function getAuthCode()
111
    {
112
        return $this->authCode;
113
    }
114
115
    /**
116
     * Sets the auth info from the message of the auth event sent by WeChat.
117
     *
118
     * @param Collection $message
119
     */
120
    public function setFromAuthMessage(Collection $message)
121
    {
122
        if ($message->has('AuthorizerAppid')) {
123
            $this->setAuthorizerAppId($message->get('AuthorizerAppid'));
124
        }
125
        if ($message->has('AuthorizationCode')) {
126
            $this->setAuthCode($message->get('AuthorizationCode'));
127
        }
128
    }
129
130
    /**
131
     * Handles authorization: calls the API, saves the tokens.
132
     *
133
     * @return Collection
134
     */
135
    public function handleAuthorization()
136
    {
137
        $info = $this->getAuthorizationInfo();
138
139
        $appId = $info['authorization_info']['authorizer_appid'];
140
        $this->setAuthorizerAppId($appId);
141
142
        $this->saveAuthorizerAccessToken($info['authorization_info']);
143
        $this->saveAuthorizerRefreshToken($info['authorization_info']);
144
145
        $authorizerInfo = $this->getAuthorizerInfo();
146
        // Duplicated info.
147
        $authorizerInfo->forget('authorization_info');
148
        $info->merge($authorizerInfo->all());
149
150
        return $info;
151
    }
152
153
    /**
154
     * Handles the authorizer access token: calls the API, saves the token.
155
     *
156
     * @return string The authorizer access token.
157
     */
158
    public function handleAuthorizerAccessToken()
159
    {
160
        $data = $this->authorizer->getAuthorizationToken(
161
            $this->getAuthorizerAppId(),
162
            $this->getAuthorizerRefreshToken()
163
        );
164
165
        $this->saveAuthorizerAccessToken($data);
166
167
        return $data['authorizer_access_token'];
168
    }
169
170
    /**
171
     * Gets the authorization information.
172
     * Like authorizer app id, access token, refresh token, function scope, etc.
173
     *
174
     * @return Collection
175
     */
176
    public function getAuthorizationInfo()
177
    {
178
        $result = $this->authorizer->getAuthorizationInfo($this->getAuthCode());
179
        if (is_array($result)) {
180
            $result = new Collection($result);
181
        }
182
183
        return $result;
184
    }
185
186
    /**
187
     * Gets the authorizer information.
188
     * Like authorizer name, logo, business, etc.
189
     *
190
     * @return Collection
191
     */
192
    public function getAuthorizerInfo()
193
    {
194
        $result = $this->authorizer->getAuthorizerInfo($this->getAuthorizerAppId());
195
        if (is_array($result)) {
196
            $result = new Collection($result);
197
        }
198
199
        return $result;
200
    }
201
202
    /**
203
     * Saves the authorizer access token in cache.
204
     *
205
     * @param Collection|array $data Array structure from WeChat API result.
206
     */
207
    public function saveAuthorizerAccessToken($data)
208
    {
209
        $accessToken = $data['authorizer_access_token'];
210
        // Expiration time, -100 to avoid any delay.
211
        $expire = $data['expires_in'] - 100;
212
213
        $this->set($this->getAuthorizerAccessTokenKey(), $accessToken, $expire);
214
    }
215
216
    /**
217
     * Gets the authorizer access token.
218
     *
219
     * @return string
220
     */
221
    public function getAuthorizerAccessToken()
222
    {
223
        return $this->get($this->getAuthorizerAccessTokenKey());
224
    }
225
226
    /**
227
     * Saves the authorizer refresh token in cache.
228
     *
229
     * @param Collection|array $data Array structure from WeChat API result.
230
     */
231
    public function saveAuthorizerRefreshToken($data)
232
    {
233
        $refreshToken = $data['authorizer_refresh_token'];
234
235
        $this->set($this->getAuthorizerRefreshTokenKey(), $refreshToken);
236
    }
237
238
    /**
239
     * Gets the authorizer refresh token.
240
     *
241
     * @return string
242
     * @throws Exception When refresh token is not present.
243
     */
244
    public function getAuthorizerRefreshToken()
245
    {
246
        if ($token = $this->get($this->getAuthorizerRefreshTokenKey())) {
247
            return $token;
248
        }
249
250
        throw new Exception(
251
            'Authorizer Refresh Token is not present, you may not make the authorization yet.'
252
        );
253
    }
254
255
    /**
256
     * Removes the authorizer access token from cache.
257
     */
258
    public function removeAuthorizerAccessToken()
259
    {
260
        $this->remove($this->getAuthorizerAccessTokenKey());
261
    }
262
263
    /**
264
     * Removes the authorizer refresh token from cache.
265
     */
266
    public function removeAuthorizerRefreshToken()
267
    {
268
        $this->remove($this->getAuthorizerRefreshTokenKey());
269
    }
270
271
    /**
272
     * Gets the authorizer access token cache key.
273
     *
274
     * @return string
275
     */
276
    public function getAuthorizerAccessTokenKey()
277
    {
278
        return self::CACHE_KEY_ACCESS_TOKEN
279
            . '.' . $this->appId
280
            . '.' . $this->getAuthorizerAppId();
281
    }
282
283
    /**
284
     * Gets the authorizer refresh token cache key.
285
     *
286
     * @return string
287
     */
288
    public function getAuthorizerRefreshTokenKey()
289
    {
290
        return self::CACHE_KEY_REFRESH_TOKEN
291
            . '.' . $this->appId
292
            . '.' . $this->getAuthorizerAppId();
293
    }
294
295
}