Completed
Pull Request — master (#122)
by
unknown
01:47
created

DouYinProvider::parseAccessToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 3
Ratio 20 %

Importance

Changes 0
Metric Value
dl 3
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Overtrue\Socialite\Providers;
4
5
use Overtrue\Socialite\AccessToken;
6
use Overtrue\Socialite\AccessTokenInterface;
7
use Overtrue\Socialite\ProviderInterface;
8
use Overtrue\Socialite\User;
9
10
/**
11
 * Class DouYinProvider.
12
 *
13
 * @author [email protected]
14
 *
15
 * @see http://open.douyin.com/platform
16
 */
17
class DouYinProvider extends AbstractProvider implements ProviderInterface
18
{
19
20
    /**
21
     * 抖音接口域名.
22
     *
23
     * @var string
24
     */
25
    protected $baseUrl = 'https://open.douyin.com';
26
27
    /**
28
     * 应用授权作用域.
29
     *
30
     * @var array
31
     */
32
    protected $scopes = ['user_info'];
33
34
    /**
35
     * 授权用户唯一标识.
36
     *
37
     * @var string
38
     */
39
    private $openId = null;
40
41
42
    /**
43
     * 获取登录页面地址.
44
     *
45
     * {@inheritdoc}
46
     */
47
    protected function getAuthUrl($state)
48
    {
49
        return $this->buildAuthUrlFromBase($this->baseUrl.'/platform/oauth/connect', $state);
50
    }
51
52
53
    /**
54
     * 获取授权码接口参数
55
     *
56
     * @param string|null $state
57
     *
58
     * @return array
59
     */
60 View Code Duplication
    public function getCodeFields($state = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        $fields = [
63
            'client_key' => $this->clientId,
64
            'redirect_uri' => $this->redirectUrl,
65
            'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
66
            'response_type' => 'code',
67
        ];
68
69
        if ($this->usesState()) {
70
            $fields['state'] = $state;
71
        }
72
73
        return $fields;
74
    }
75
76
    /**
77
     * 获取access_token地址.
78
     *
79
     * {@inheritdoc}
80
     */
81
    protected function getTokenUrl()
82
    {
83
        return $this->baseUrl.'/oauth/access_token';
84
    }
85
86
    /**
87
     * 通过code获取access_token
88
     *
89
     * @param string $code
90
     *
91
     * @return \Overtrue\Socialite\AccessToken
92
     */
93 View Code Duplication
    public function getAccessToken($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $response = $this->getHttpClient()->get($this->getTokenUrl(), [
96
            'query' => $this->getTokenFields($code),
97
        ]);
98
99
        return $this->parseAccessToken($response->getBody()->getContents());
0 ignored issues
show
Documentation introduced by
$response->getBody()->getContents() is of type string, but the function expects a object<Psr\Http\Message\StreamInterface>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
    }
101
102
    /**
103
     * 获取access_token接口参数
104
     *
105
     * @param string $code
106
     *
107
     * @return array
108
     */
109
    protected function getTokenFields($code)
110
    {
111
        return [
112
            'client_key' => $this->clientId,
113
            'client_secret' => $this->clientSecret,
114
            'code' => $code,
115
            'grant_type' => 'authorization_code',
116
        ];
117
    }
118
119
    /**
120
     * 格式化token
121
     *
122
     * @param \Psr\Http\Message\StreamInterface|array $body
123
     *
124
     * @return \Overtrue\Socialite\AccessTokenInterface
125
     */
126
    protected function parseAccessToken($body)
127
    {
128
        if (!is_array($body)) {
129
            $body = json_decode($body, true);
130
        }
131
132 View Code Duplication
        if (empty($body['data']['access_token'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            throw new AuthorizeFailedException('Authorize Failed: '.json_encode($body, JSON_UNESCAPED_UNICODE), $body);
134
        }
135
136
        // 抖音用户唯一标识
137
        $this->openId = $body['data']['open_id'];
138
139
        return new AccessToken($body['data']);
140
    }
141
142
    /**
143
     * 通过token 获取用户信息
144
     *
145
     * @param AccessTokenInterface $token
146
     * @return array|mixed
147
     */
148
    protected function getUserByToken(AccessTokenInterface $token)
149
    {
150
        $userUrl = $this->baseUrl.'/oauth/userinfo/';
151
152
        $response = $this->getHttpClient()->get(
153
            $userUrl, [
154
                'query' => ['access_token' => $token->getToken(),
155
                    'open_id' => $this->openId
156
                ],
157
            ]
158
        );
159
160
        return json_decode($response->getBody(), true);
161
    }
162
163
    /**
164
     * 格式化用户信息
165
     *
166
     * @param array $user
167
     * 
168
     * @return User
169
     */
170
    protected function mapUserToObject(array $user)
171
    {
172
        return new User([
173
            'id' => $this->arrayItem($user, 'open_id'),
174
            'username' => $this->arrayItem($user, 'nickname'),
175
            'nickname' => $this->arrayItem($user, 'nickname'),
176
            'avatar' => $this->arrayItem($user, 'avatar'),
177
        ]);
178
    }
179
180
}
181