Completed
Push — master ( 017947...8f5b55 )
by frey
13s
created

App   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 25.32 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 20
loc 79
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthUrl() 0 4 1
A buildAuthUrlFromBase() 0 6 1
A getCodeFields() 0 10 2
A user() 12 12 3
A detail() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace EntWeChat\Auth;
4
5
use EntWeChat\Core\Exceptions\InvalidStateException;
6
7
/**
8
 * Class App.
9
 */
10
class App extends AbstractAuthentication
11
{
12
    const AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize';
13
    const API_GET_USER_INFO = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo';
14
    const API_GET_USER_DETAIL = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail';
15
16
    /**
17
     * Indicates if the session state should be utilized.
18
     *
19
     * @var bool
20
     */
21
    protected $stateless = true;
22
23
    /**
24
     * {@inheritdoc}.
25
     */
26
    protected function getAuthUrl($state)
27
    {
28
        return $this->buildAuthUrlFromBase(self::AUTH_URL, $state);
29
    }
30
31
    /**
32
     * {@inheritdoc}.
33
     */
34
    protected function buildAuthUrlFromBase($url, $state)
35
    {
36
        $query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType);
37
38
        return $url.'?'.$query.'#wechat_redirect';
39
    }
40
41
    /**
42
     * {@inheritdoc}.
43
     */
44
    protected function getCodeFields($state = null)
45
    {
46
        return array_merge([
47
            'appid'         => $this->clientId,
48
            'redirect_uri'  => $this->redirectUrl,
49
            'response_type' => 'code',
50
            'scope'         => $this->formatScopes($this->scopes, $this->scopeSeparator),
51
            'state'         => $state ?: md5(time()),
52
        ], $this->parameters);
53
    }
54
55
    /**
56
     * @param null $code
57
     *
58
     * @throws InvalidStateException
59
     *
60
     * @return \EntWeChat\Support\Collection
61
     */
62 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...
63
    {
64
        if ($this->hasInvalidState()) {
65
            throw new InvalidStateException();
66
        }
67
68
        $params = [
69
            'code' => $code ?: $this->request->get('code'),
70
        ];
71
72
        return $this->parseJSON('get', [self::API_GET_USER_INFO, $params]);
73
    }
74
75
    /**
76
     * @param $user_ticket
77
     *
78
     * @return \EntWeChat\Support\Collection
79
     */
80 View Code Duplication
    public function detail($user_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...
81
    {
82
        $params = [
83
            'user_ticket' => $user_ticket,
84
        ];
85
86
        return $this->parseJSON('post', [self::API_GET_USER_DETAIL, $params]);
87
    }
88
}
89