Completed
Push — master ( f182ae...6d72d4 )
by Carlos
01:35
created

BaiduProvider::getCodeFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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\AccessTokenInterface;
6
use Overtrue\Socialite\ProviderInterface;
7
use Overtrue\Socialite\User;
8
9
/**
10
 * Class BaiduProvider.
11
 *
12
 * @see https://developer.baidu.com/wiki/index.php?title=docs/oauth [OAuth 2.0 授权机制说明]
13
 */
14
class BaiduProvider extends AbstractProvider implements ProviderInterface
15
{
16
    /**
17
     * The base url of Weibo API.
18
     *
19
     * @var string
20
     */
21
    protected $baseUrl = 'https://openapi.baidu.com';
22
23
    /**
24
     * The API version for the request.
25
     *
26
     * @var string
27
     */
28
    protected $version = '2.0';
29
30
    /**
31
     * The scopes being requested.
32
     *
33
     * @var array
34
     */
35
    protected $scopes = [''];
36
37
    /**
38
     * The uid of user authorized.
39
     *
40
     * @var int
41
     */
42
    protected $uid;
43
44
    protected $display = 'popup';
45
46
    /**
47
     * Get the authentication URL for the provider.
48
     *
49
     * @param string $state
50
     *
51
     * @return string
52
     */
53
    protected function getAuthUrl($state)
54
    {
55
        return $this->buildAuthUrlFromBase($this->baseUrl . '/oauth/' . $this->version . '/authorize', $state);
56
    }
57
58
    /**
59
     * {@inheritdoc}.
60
     */
61
    protected function getCodeFields($state = null)
62
    {
63
        return array_merge([
64
            'response_type' => 'code',
65
            'client_id'     => $this->clientId,
66
            'redirect_uri'  => $this->redirectUrl,
67
            'scope'         => $this->formatScopes($this->scopes, $this->scopeSeparator),
68
            'display'       => $this->display,
69
        ], $this->parameters);
70
    }
71
72
    /**
73
     * Get the token URL for the provider.
74
     *
75
     * @return string
76
     */
77
    protected function getTokenUrl()
78
    {
79
        return $this->baseUrl . '/oauth/' . $this->version . '/token';
80
    }
81
82
    /**
83
     * Get the Post fields for the token request.
84
     *
85
     * @param string $code
86
     *
87
     * @return array
88
     */
89
    protected function getTokenFields($code)
90
    {
91
        return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
92
    }
93
94
    /**
95
     * Get the raw user for the given access token.
96
     *
97
     * @param \Overtrue\Socialite\AccessTokenInterface $token
98
     *
99
     * @return array
100
     */
101 View Code Duplication
    protected function getUserByToken(AccessTokenInterface $token)
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...
102
    {
103
        $response = $this->getHttpClient()->get($this->baseUrl . '/rest/' . $this->version . '/passport/users/getInfo', [
104
            'query'   => [
105
                'access_token' => $token->getToken(),
106
            ],
107
            'headers' => [
108
                'Accept' => 'application/json',
109
            ],
110
        ]);
111
112
        return json_decode($response->getBody(), true);
113
    }
114
115
    /**
116
     * Map the raw user array to a Socialite User instance.
117
     *
118
     * @param array $user
119
     *
120
     * @return \Overtrue\Socialite\User
121
     */
122
    protected function mapUserToObject(array $user)
123
    {
124
        $realname = $this->arrayItem($user, 'realname');
125
        return new User([
126
            'id'       => $this->arrayItem($user, 'userid'),
127
            'nickname' => empty($realname) ? '' : $realname,
128
            'name'     => $this->arrayItem($user, 'username'),
129
            'email'    => '',
130
            'avatar'   => $this->arrayItem($user, 'portrait'),
131
        ]);
132
    }
133
}
134