Completed
Push — master ( 73ed3f...e38e9f )
by Carlos
02:46
created

AccessToken   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 19
loc 54
rs 10
c 0
b 0
f 0
ccs 0
cts 18
cp 0
wmc 3
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenFromServer() 18 18 2
A getComponentVerifyTicket() 0 4 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
/*
4
 * This file is part of the overtrue/wechat.
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
/**
13
 * AccessToken.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\OpenPlatform;
28
29
use EasyWeChat\Core\AccessToken as WechatAccessToken;
30
use EasyWeChat\Core\Exceptions\HttpException;
31
32
class AccessToken extends WechatAccessToken
33
{
34
    /**
35
     * API.
36
     */
37
    const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
38
39
    /**
40
     * {@inheritdoc}.
41
     */
42
    protected $queryName = 'component_access_token';
43
44
    /**
45
     * {@inheritdoc}.
46
     */
47
    protected $tokenJsonKey = 'component_access_token';
48
49
    /**
50
     * {@inheritdoc}.
51
     */
52
    protected $prefix = 'easywechat.common.component_access_token.';
53
54
    /**
55
     * {@inheritdoc}.
56
     */
57 View Code Duplication
    public function getTokenFromServer()
58
    {
59
        $data = [
60
            'component_appid' => $this->appId,
61
            'component_appsecret' => $this->secret,
62
            'component_verify_ticket' => $this->getComponentVerifyTicket(),
63
        ];
64
65
        $http = $this->getHttp();
66
67
        $token = $http->parseJSON($http->json(self::API_TOKEN_GET, $data));
68
69
        if (empty($token[$this->tokenJsonKey])) {
70
            throw new HttpException('Request ComponentAccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE));
71
        }
72
73
        return $token;
74
    }
75
76
    /**
77
     * Get component verify ticket.
78
     *
79
     * @return string
80
     */
81
    private function getComponentVerifyTicket()
82
    {
83
        return VerifyTicket::getTicket($this->appId);
84
    }
85
}
86