Completed
Pull Request — master (#122)
by
unknown
03:41
created

DouYinProvider::parseAccessToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 3
Ratio 21.43 %

Importance

Changes 0
Metric Value
dl 3
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php namespace Overtrue\Socialite\Providers;
2
3
use Overtrue\Socialite\AccessToken;
4
use Overtrue\Socialite\AccessTokenInterface;
5
use Overtrue\Socialite\ProviderInterface;
6
use Overtrue\Socialite\User;
7
8
9
/**
10
 * Class DouYinProvider.
11
 *
12
 * @author [email protected]
13
 * @see http://open.douyin.com/platform
14
 */
15
class DouYinProvider extends AbstractProvider implements ProviderInterface
16
{
17
18
    /**
19
     * 抖音接口域名
20
     *
21
     * @var string
22
     */
23
    protected $baseUrl = 'https://open.douyin.com';
24
25
    /**
26
     * 应用授权作用域
27
     *
28
     * @var array
29
     */
30
    protected $scopes = ['user_info'];
31
32
    /**
33
     * 授权用户唯一标识
34
     *
35
     * @var string
36
     */
37
    private $openId = null;
38
39
40
    /**
41
     * 获取登录页面地址
42
     *
43
     * {@inheritdoc}
44
     */
45
    protected function getAuthUrl($state)
46
    {
47
        return $this->buildAuthUrlFromBase($this->baseUrl . '/platform/oauth/connect', $state);
48
    }
49
50
51
    /**
52
     * 获取授权码接口参数
53
     *
54
     * @param string|null $state
55
     *
56
     * @return array
57
     */
58 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...
59
    {
60
        $fields = [
61
            'client_key' => $this->clientId,
62
            'redirect_uri' => $this->redirectUrl,
63
            'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
64
            'response_type' => 'code',
65
        ];
66
67
        if ($this->usesState()) {
68
            $fields['state'] = $state;
69
        }
70
71
        return $fields;
72
    }
73
74
    /**
75
     * 获取access_token地址
76
     *
77
     * {@inheritdoc}
78
     */
79
    protected function getTokenUrl()
80
    {
81
        return $this->baseUrl . '/oauth/access_token';
82
    }
83
84
    /**
85
     * 通过code获取access_token
86
     *
87
     * @param string $code
88
     *
89
     * @return \Overtrue\Socialite\AccessToken
90
     */
91 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...
92
    {
93
        $response = $this->getHttpClient()->get($this->getTokenUrl(), [
94
            'query' => $this->getTokenFields($code),
95
        ]);
96
97
        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...
98
    }
99
100
    /**
101
     * 获取access_token接口参数
102
     *
103
     * @param string $code
104
     *
105
     * @return array
106
     */
107
    protected function getTokenFields($code)
108
    {
109
        return [
110
            'client_key' => $this->clientId,
111
            'client_secret' => $this->clientSecret,
112
            'code' => $code,
113
            'grant_type' => 'authorization_code',
114
        ];
115
    }
116
117
    /**
118
     * 格式化token
119
     *
120
     * @param \Psr\Http\Message\StreamInterface|array $body
121
     *
122
     * @return \Overtrue\Socialite\AccessTokenInterface
123
     */
124
    protected function parseAccessToken($body) {
125
        if (!is_array($body)) {
126
            $body = json_decode($body, true);
127
        }
128
129 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...
130
            throw new AuthorizeFailedException('Authorize Failed: '.json_encode($body, JSON_UNESCAPED_UNICODE), $body);
131
        }
132
133
        // 抖音用户唯一标识
134
        $this->openId = $body['data']['open_id'];
135
136
        return new AccessToken($body['data']);
137
    }
138
139
    /**
140
     * 通过token 获取用户信息
141
     *
142
     * @param AccessTokenInterface $token
143
     * @return array|mixed
144
     */
145
    protected function getUserByToken(AccessTokenInterface $token)
146
    {
147
        $userUrl = $this->baseUrl . '/oauth/userinfo/';
148
149
        $response = $this->getHttpClient()->get(
150
            $userUrl, [
151
                'query' => ['access_token' => $token->getToken(),
152
                    'open_id' => $this->openId
153
                ],
154
            ]
155
        );
156
157
        return json_decode($response->getBody(), true);;
158
    }
159
160
    /**
161
     * 格式化用户信息
162
     *
163
     * @param array $user
164
     * @return User
165
     */
166
    protected function mapUserToObject(array $user)
167
    {
168
        return new User([
169
            'id' => $this->arrayItem($user, 'open_id'),
170
            'username' => $this->arrayItem($user, 'nickname'),
171
            'nickname' => $this->arrayItem($user, 'nickname'),
172
            'avatar' => $this->arrayItem($user, 'avatar'),
173
        ]);
174
    }
175
176
}
177