Token   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 1
b 0
f 0
dl 0
loc 54
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequestPath() 0 3 1
A getRequestMethod() 0 3 1
A getToken() 0 12 5
1
<?php
2
3
4
namespace MuCTS\Sobot\Token;
5
6
use MuCTS\Sobot\Contracts\Sobot;
7
use MuCTS\Sobot\Token\Token\Response;
8
9
/**
10
 * Class Token
11
 * @mixin \MuCTS\Sobot\Token\Token\Request
12
 * @package MuCTS\Sobot\Token
13
 */
14
class Token extends Sobot
15
{
16
    /**
17
     * 接口请求方式
18
     *
19
     * @return string
20
     * @author herry.yao <[email protected]>
21
     * @version 1.2.2
22
     * @date 2020-08-05
23
     */
24
    public function getRequestMethod(): string
25
    {
26
        return self::METHOD_GET;
27
    }
28
29
30
    /**
31
     * 接口请求地址
32
     *
33
     * @return string
34
     * @author herry.yao <[email protected]>
35
     * @version 1.2.2
36
     * @date 2020-08-05
37
     */
38
    public function getRequestPath(): string
39
    {
40
        return 'api/get_token';
41
    }
42
43
    /**
44
     * 获取智齿TOKEN
45
     *
46
     * @param bool $updating
47
     * @return mixed|string
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     * @throws \MuCTS\Sobot\Exceptions\ResponseException
50
     * @throws \MuCTS\Sobot\Exceptions\SobotException|
51
     * @throws \MuCTS\Sobot\Exceptions\ConfigException
52
     * @author herry.yao <[email protected]>
53
     * @version 1.2.2
54
     * @date 2020-08-05
55
     */
56
    public function getToken($updating = false)
57
    {
58
        $key = 'sobot_' . substr(md5(json_encode([__METHOD__, $this->getAppId()])), -5);
59
        if (!is_null($this->cache) && !$updating && $this->cache->exists($key)) {
60
            return $this->cache->get($key);
61
        }
62
        /** @var Response $res */
63
        $res     = $this->request();
64
        $token   = $res->item->token;
65
        $expires = $res->item->expires_in;
66
        !is_null($this->cache) && $this->cache->set($key, $token, $expires);
67
        return $token;
68
    }
69
}