WorkWeb::getAuthUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Auth;
4
5
use EntWeChat\Core\Exceptions\InvalidStateException;
6
7
/**
8
 * Class WorkWeb.
9
 */
10
class WorkWeb extends AbstractAuthentication
11
{
12
    const AUTH_URL = 'https://open.work.weixin.qq.com/wwopen/sso/qrConnect';
13
    const API_GET_USER_INFO = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo';
14
15
    /**
16
     * Indicates if the session state should be utilized.
17
     *
18
     * @var bool
19
     */
20
    protected $stateless = true;
21
22
    /**
23
     * {@inheritdoc}.
24
     */
25
    protected function getAuthUrl($state)
26
    {
27
        return $this->buildAuthUrlFromBase(self::AUTH_URL, $state);
28
    }
29
30
    /**
31
     * {@inheritdoc}.
32
     */
33
    protected function buildAuthUrlFromBase($url, $state)
34
    {
35
        $query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType);
36
37
        return $url.'?'.$query;
38
    }
39
40
    /**
41
     * {@inheritdoc}.
42
     */
43
    protected function getCodeFields($state = null)
44
    {
45
        return array_merge([
46
            'appid'         => $this->clientId,
47
            'redirect_uri'  => $this->redirectUrl,
48
            'state'         => $state ?: md5(time()),
49
        ], $this->parameters);
50
    }
51
52
    /**
53
     * @param null $code
54
     *
55
     * @throws InvalidStateException
56
     *
57
     * @return \EntWeChat\Support\Collection
58
     */
59 View Code Duplication
    public function user($code = 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...
60
    {
61
        if ($this->hasInvalidState()) {
62
            throw new InvalidStateException();
63
        }
64
65
        $params = [
66
            'code' => $code ?: $this->request->get('code'),
67
        ];
68
69
        return $this->parseJSON('get', [self::API_GET_USER_INFO, $params]);
70
    }
71
}
72