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