Completed
Pull Request — master (#581)
by
unknown
02:40
created

AccessToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
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 Doctrine\Common\Cache\Cache;
30
use EasyWeChat\Core\AccessToken as WechatAccessToken;
31
use EasyWeChat\Core\Exceptions\HttpException;
32
use EasyWeChat\OpenPlatform\Traits\VerifyTicketTrait;
33
use EasyWeChat\OpenPlatform\VerifyTicket;
34
35
class AccessToken extends WechatAccessToken
36
{
37
    use VerifyTicketTrait;
38
39
    /**
40
     * API.
41
     */
42
    const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
43
44
    /**
45
     * {@inheritdoc}.
46
     */
47
    protected $queryName = 'component_access_token';
48
49
    /**
50
     * {@inheritdoc}.
51
     */
52
    protected $tokenJsonKey = 'component_access_token';
53
54
    /**
55
     * {@inheritdoc}.
56
     */
57
    protected $prefix = 'easywechat.common.component_access_token.';
58
59
    /**
60
     * AccessToken constructor.
61
     *
62
     * @param string       $appId
63
     * @param string       $secret
64
     * @param Cache        $cache
65
     * @param VerifyTicket $verifyTicket
66
     */
67
    public function __construct($appId, $secret, VerifyTicket $verifyTicket, Cache $cache = null)
68
    {
69
        parent::__construct($appId, $secret, $cache);
70
71
        $this->setVerifyTicket($verifyTicket);
72
    }
73
74
    /**
75
     * {@inheritdoc}.
76
     */
77 View Code Duplication
    public function getTokenFromServer()
78
    {
79
        $data = [
80
            'component_appid' => $this->appId,
81
            'component_appsecret' => $this->secret,
82
            'component_verify_ticket' => $this->verifyTicket->getTicket(),
83
        ];
84
85
        $http = $this->getHttp();
86
87
        $token = $http->parseJSON($http->json(self::API_TOKEN_GET, $data));
88
89
        if (empty($token[$this->tokenJsonKey])) {
90
            throw new HttpException('Request ComponentAccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE));
91
        }
92
93
        return $token;
94
    }
95
}
96