Completed
Pull Request — master (#553)
by
unknown
02:23
created

AccessToken::getTokenFromServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
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
70
        if (empty($token[$this->tokenJsonKey])) {
71
            throw new HttpException('Request ComponentAccessToken fail. response: ' . json_encode($token, JSON_UNESCAPED_UNICODE));
72
        }
73
74
        return $token;
75
    }
76
77
    /**
78
     * Get component verify ticket.
79
     *
80
     * @return string
81
     */
82
    private function getComponentVerifyTicket()
83
    {
84
        return VerifyTicket::getTicket($this->appId);
85
    }
86
}
87