Completed
Push — master ( 311c78...0ac41d )
by Carlos
04:30 queued 51s
created

Authorization::getAuthorizerAppId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

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