Completed
Pull Request — master (#122)
by
unknown
02:04
created

DouYinProvider::getAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
     * {@inheritdoc}
38
     */
39
    protected function getAuthUrl($state)
40
    {
41
        return $this->buildAuthUrlFromBase($this->baseUrl.'/platform/oauth/connect', $state);
42
    }
43
44
45
    /**
46
     * 获取授权码接口参数
47
     *
48
     * @param string|null $state
49
     *
50
     * @return array
51
     */
52 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...
53
    {
54
        $fields = [
55
            'client_key' => $this->clientId,
56
            'redirect_uri' => $this->redirectUrl,
57
            'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
58
            'response_type' => 'code',
59
        ];
60
61
        if ($this->usesState()) {
62
            $fields['state'] = $state;
63
        }
64
65
        return $fields;
66
    }
67
68
    /**
69
     * 获取access_token地址.
70
     *
71
     * {@inheritdoc}
72
     */
73
    protected function getTokenUrl()
74
    {
75
        return $this->baseUrl.'/oauth/access_token';
76
    }
77
78
    /**
79
     * 通过code获取access_token
80
     *
81
     * @param string $code
82
     *
83
     * @return \Overtrue\Socialite\AccessToken
84
     */
85 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...
86
    {
87
        $response = $this->getHttpClient()->get($this->getTokenUrl(), [
88
            'query' => $this->getTokenFields($code),
89
        ]);
90
91
        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...
92
    }
93
94
    /**
95
     * 获取access_token接口参数
96
     *
97
     * @param string $code
98
     *
99
     * @return array
100
     */
101
    protected function getTokenFields($code)
102
    {
103
        return [
104
            'client_key' => $this->clientId,
105
            'client_secret' => $this->clientSecret,
106
            'code' => $code,
107
            'grant_type' => 'authorization_code',
108
        ];
109
    }
110
111
    /**
112
     * 格式化token
113
     *
114
     * @param \Psr\Http\Message\StreamInterface|array $body
115
     *
116
     * @return \Overtrue\Socialite\AccessTokenInterface
117
     */
118 View Code Duplication
    protected function parseAccessToken($body)
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...
119
    {
120
        if (!is_array($body)) {
121
            $body = json_decode($body, true);
122
        }
123
124
        if (empty($body['data']['access_token'])) {
125
            throw new AuthorizeFailedException('Authorize Failed: '.json_encode($body, JSON_UNESCAPED_UNICODE), $body);
126
        }
127
128
        return new AccessToken($body['data']);
129
    }
130
131
    /**
132
     * 通过token 获取用户信息
133
     *
134
     * @param AccessTokenInterface $token
135
     * @return array|mixed
136
     */
137
    protected function getUserByToken(AccessTokenInterface $token)
138
    {
139
        $userUrl = $this->baseUrl.'/oauth/userinfo/';
140
141
        $response = $this->getHttpClient()->get(
142
            $userUrl, [
143
                'query' => [
144
                    'access_token' => $token->getToken(),
145
                    'open_id' => $token['open_id']
146
                ],
147
            ]
148
        );
149
150
        return json_decode($response->getBody(), true);
151
    }
152
153
    /**
154
     * 格式化用户信息
155
     *
156
     * @param array $user
157
     *
158
     * @return User
159
     */
160
    protected function mapUserToObject(array $user)
161
    {
162
        return new User([
163
            'id' => $this->arrayItem($user, 'open_id'),
164
            'username' => $this->arrayItem($user, 'nickname'),
165
            'nickname' => $this->arrayItem($user, 'nickname'),
166
            'avatar' => $this->arrayItem($user, 'avatar'),
167
        ]);
168
    }
169
170
}
171