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

WeWorkProvider::detailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
     * @param string $agentId
51
     *
52
     * @return $this
53
     */
54
    public function agent($agentId)
55
    {
56
        return $this->setAgentId($agentId);
57
    }
58
59
    /**
60
     * Return user details.
61
     *
62
     * @return $this
63
     */
64
    public function detailed()
65
    {
66
        $this->detailed = true;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $state
73
     *
74
     * @return string
75
     */
76
    protected function getAuthUrl($state)
77
    {
78
        // 网页授权登录
79
        if (!empty($this->scopes)) {
80
            return $this->getOAuthUrl($state);
81
        }
82
83
        // 第三方网页应用登录(扫码登录)
84
        return $this->getQrConnectUrl($state);
85
    }
86
87
    /**
88
     * OAuth url.
89
     *
90
     * @param string $state
91
     *
92
     * @return string
93
     */
94
    protected function getOAuthUrl($state)
95
    {
96
        $queries = [
97
            'appid' => $this->clientId,
98
            'redirect_uri' => $this->redirectUrl,
99
            'response_type' => 'code',
100
            'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
101
            'agentid' => $this->agentId,
102
            'state' => $state,
103
        ];
104
105
        return sprintf('https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect', http_build_query($queries));
106
    }
107
108
    /**
109
     * Qr connect url.
110
     *
111
     * @param string $state
112
     *
113
     * @return string
114
     */
115
    protected function getQrConnectUrl($state)
116
    {
117
        $queries = [
118
            'appid' => $this->clientId,
119
            'agentid' => $this->agentId,
120
            'redirect_uri' => $this->redirectUrl,
121
            'state' => $state,
122
        ];
123
124
        return 'https://open.work.weixin.qq.com/wwopen/sso/qrConnect?'.http_build_query($queries);
125
    }
126
127
    protected function getTokenUrl()
128
    {
129
        return null;
130
    }
131
132
    /**
133
     * @param \Overtrue\Socialite\AccessTokenInterface $token
134
     *
135
     * @return mixed
136
     */
137
    protected function getUserByToken(AccessTokenInterface $token)
138
    {
139
        $userInfo = $this->getUserInfo($token);
140
141
        if ($this->detailed && isset($userInfo['user_ticket'])) {
142
            return $this->getUserDetail($token, $userInfo['user_ticket']);
143
        }
144
145
        $this->detailed = false;
146
147
        return $userInfo;
148
    }
149
150
    /**
151
     * Get user base info.
152
     *
153
     * @param \Overtrue\Socialite\AccessTokenInterface $token
154
     *
155
     * @return mixed
156
     */
157 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...
158
    {
159
        $response = $this->getHttpClient()->get('https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo', [
160
            'query' => array_filter([
161
                'access_token' => $token->getToken(),
162
                'code' => $this->getCode(),
163
            ]),
164
        ]);
165
166
        return json_decode($response->getBody(), true);
167
    }
168
169
    /**
170
     * Get user detail info.
171
     *
172
     * @param \Overtrue\Socialite\AccessTokenInterface $token
173
     * @param $ticket
174
     *
175
     * @return mixed
176
     */
177 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...
178
    {
179
        $response = $this->getHttpClient()->post('https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail', [
180
            'query' => [
181
                'access_token' => $token->getToken(),
182
            ],
183
            'json' => [
184
                'user_ticket' => $ticket,
185
            ],
186
        ]);
187
188
        return json_decode($response->getBody(), true);
189
    }
190
191
    /**
192
     * @param array $user
193
     *
194
     * @return \Overtrue\Socialite\User
195
     */
196
    protected function mapUserToObject(array $user)
197
    {
198
        if ($this->detailed) {
199
            return new User([
200
                'id' => $this->arrayItem($user, 'userid'),
201
                'name' => $this->arrayItem($user, 'name'),
202
                'avatar' => $this->arrayItem($user, 'avatar'),
203
                'email' => $this->arrayItem($user, 'email'),
204
            ]);
205
        }
206
207
        return new User(array_filter([
208
            'id' => $this->arrayItem($user, 'UserId') ?: $this->arrayItem($user, 'OpenId'),
209
            'userId' => $this->arrayItem($user, 'UserId'),
210
            'openid' => $this->arrayItem($user, 'OpenId'),
211
            'deviceId' => $this->arrayItem($user, 'DeviceId'),
212
        ]));
213
    }
214
}
215