Completed
Pull Request — master (#66)
by mingyoung
01:39
created

WeWorkProvider::mapUserToObject()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Socialite\Providers;
13
14
use Overtrue\Socialite\AccessTokenInterface;
15
use Overtrue\Socialite\ProviderInterface;
16
use Overtrue\Socialite\User;
17
18
/**
19
 * Class WeWorkProvider.
20
 *
21
 * @author mingyoung <[email protected]>
22
 */
23
class WeWorkProvider extends AbstractProvider implements ProviderInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $agentId;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $detailed = false;
34
35
    /**
36
     * Set agent id.
37
     *
38
     * @param string $agentId
39
     *
40
     * @return $this
41
     */
42
    public function setAgentId($agentId)
43
    {
44
        $this->agentId = $agentId;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Return user details.
51
     *
52
     * @return $this
53
     */
54
    public function detailed()
55
    {
56
        $this->detailed = true;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $state
63
     *
64
     * @return string
65
     */
66
    protected function getAuthUrl($state)
67
    {
68
        // 网页授权登录
69
        if (!empty($this->scopes)) {
70
            return $this->getOAuthUrl($state);
71
        }
72
73
        // 第三方网页应用登录(扫码登录)
74
        return $this->getQrConnectUrl($state);
75
    }
76
77
    /**
78
     * OAuth url.
79
     *
80
     * @param string $state
81
     *
82
     * @return string
83
     */
84
    protected function getOAuthUrl($state)
85
    {
86
        $queries = [
87
            'appid' => $this->clientId,
88
            'redirect_uri' => $this->redirectUrl,
89
            'response_type' => 'code',
90
            'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
91
            'agentid' => $this->agentId,
92
            'state' => $state,
93
        ];
94
95
        return sprintf('https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect', http_build_query($queries));
96
    }
97
98
    /**
99
     * Qr connect url.
100
     *
101
     * @param string $state
102
     *
103
     * @return string
104
     */
105
    protected function getQrConnectUrl($state)
106
    {
107
        $queries = [
108
            'appid' => $this->clientId,
109
            'agentid' => $this->agentId,
110
            'redirect_uri' => $this->redirectUrl,
111
            'state' => $state,
112
        ];
113
114
        return 'https://open.work.weixin.qq.com/wwopen/sso/qrConnect?'.http_build_query($queries);
115
    }
116
117
    protected function getTokenUrl()
118
    {
119
        return null;
120
    }
121
122
    /**
123
     * @param \Overtrue\Socialite\AccessTokenInterface $token
124
     *
125
     * @return mixed
126
     */
127
    protected function getUserByToken(AccessTokenInterface $token)
128
    {
129
        $userInfo = $this->getUserInfo($token);
130
131
        if ($this->detailed && isset($userInfo['user_ticket'])) {
132
            return $this->getUserDetail($token, $userInfo['user_ticket']);
133
        }
134
135
        $this->detailed = false;
136
137
        return $userInfo;
138
    }
139
140
    /**
141
     * Get user base info.
142
     *
143
     * @param \Overtrue\Socialite\AccessTokenInterface $token
144
     *
145
     * @return mixed
146
     */
147 View Code Duplication
    protected function getUserInfo(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...
148
    {
149
        $response = $this->getHttpClient()->get('https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo', [
150
            'query' => array_filter([
151
                'access_token' => $token->getToken(),
152
                'code' => $this->getCode(),
153
            ]),
154
        ]);
155
156
        return json_decode($response->getBody(), true);
157
    }
158
159
    /**
160
     * Get user detail info.
161
     *
162
     * @param \Overtrue\Socialite\AccessTokenInterface $token
163
     * @param $ticket
164
     *
165
     * @return mixed
166
     */
167 View Code Duplication
    protected function getUserDetail(AccessTokenInterface $token, $ticket)
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...
168
    {
169
        $response = $this->getHttpClient()->post('https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail', [
170
            'query' => [
171
                'access_token' => $token->getToken(),
172
            ],
173
            'json' => [
174
                'user_ticket' => $ticket,
175
            ],
176
        ]);
177
178
        return json_decode($response->getBody(), true);
179
    }
180
181
    /**
182
     * @param array $user
183
     *
184
     * @return \Overtrue\Socialite\User
185
     */
186
    protected function mapUserToObject(array $user)
187
    {
188
        if ($this->detailed) {
189
            return new User([
190
                'id' => $this->arrayItem($user, 'userid'),
191
                'name' => $this->arrayItem($user, 'name'),
192
                'avatar' => $this->arrayItem($user, 'avatar'),
193
                'email' => $this->arrayItem($user, 'email'),
194
            ]);
195
        }
196
197
        return new User(array_filter([
198
            'id' => $this->arrayItem($user, 'UserId') ?: $this->arrayItem($user, 'OpenId'),
199
            'userId' => $this->arrayItem($user, 'UserId'),
200
            'openid' => $this->arrayItem($user, 'OpenId'),
201
            'deviceId' => $this->arrayItem($user, 'DeviceId'),
202
        ]));
203
    }
204
}
205