Passed
Push — master ( e5b474...40007f )
by wannanbigpig
02:47
created

Client   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 29
c 1
b 0
f 0
dl 0
loc 102
ccs 27
cts 27
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loginAuthorization() 0 13 2
A getUserInfo() 0 8 1
A getAccessToken() 0 15 2
A getAuthorizationUrl() 0 13 2
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay-sdk-php.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace EasyAlipay\BaseService\Auth;
12
13
use EasyAlipay\Kernel\Support\Support;
14
15
class Client extends Support
16
{
17
    /**
18
     * alipay.system.oauth.token(换取授权访问令牌).
19
     *
20
     * @param string $code
21
     * @param string $grantType
22
     *
23
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
24
     *
25
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
26
     * @throws \GuzzleHttp\Exception\GuzzleException
27
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
28
     */
29 2
    public function getAccessToken(string $code, string $grantType = 'authorization_code')
30
    {
31 2
        $method = 'alipay.system.oauth.token';
32
33
        $params = [
34 2
            'grant_type' => $grantType,
35
        ];
36
37 2
        if ($grantType !== 'authorization_code') {
38 2
            $params['refresh_token'] = $code;
39
        } else {
40 2
            $params['code'] = $code;
41
        }
42
43 2
        return $this->request($method, $params);
44
    }
45
46
    /**
47
     * get authorization url.
48
     *
49
     * @param string      $redirectUri
50
     * @param string      $scope
51
     * @param string|null $state
52
     *
53
     * @return string
54
     */
55 2
    public function getAuthorizationUrl(string $redirectUri, string $scope = 'auth_base', string $state = null)
56
    {
57 2
        $appid = $this->app->config->get('sys_params.app_id');
58
        $params = [
59 2
            'app_id' => $appid,
60 2
            'scope' => $scope,
61 2
            'redirect_uri' => $redirectUri,
62 2
            'state' => $state ?: $scope,
63
        ];
64
65 2
        $url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?';
66
67 2
        return $url.http_build_query($params);
68
    }
69
70
    /**
71
     * alipay.user.info.share(支付宝会员授权信息查询接口).
72
     *
73
     * @param string $authToken
74
     *
75
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
76
     *
77
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
78
     * @throws \GuzzleHttp\Exception\GuzzleException
79
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
80
     */
81 2
    public function getUserInfo(string $authToken)
82
    {
83 2
        $method = 'alipay.user.info.share';
84
        $params = [
85 2
            'auth_token' => $authToken,
86
        ];
87
88 2
        return $this->request($method, $params);
89
    }
90
91
    /**
92
     * alipay.user.info.auth(用户登陆授权).
93
     *
94
     * @param string      $state
95
     * @param string      $scopes
96
     * @param string|null $returnUrl
97
     *
98
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
99
     *
100
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidSignException
101
     * @throws \GuzzleHttp\Exception\GuzzleException
102
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
103
     */
104 2
    public function loginAuthorization(string $state, string $scopes = 'auth_base', string $returnUrl = null)
105
    {
106 2
        $method = 'alipay.user.info.auth';
107
        $params = [
108 2
            'scopes' => $scopes,
109 2
            'state' => $state,
110
        ];
111 2
        $this->app->setEndpointConfig($method, [
112 2
            'return_url' => $returnUrl ?: $this->app['config']->get('return_url'),
113
        ]);
114
115 2
        return $this->request($method, [
116 2
            'biz_content' => $params,
117
        ]);
118
    }
119
}
120