Passed
Push — master ( 273c7d...4c8747 )
by ma
02:37
created

GatewayTrait::setCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
namespace tinymeng\OAuth2\Connector;
3
4
trait GatewayTrait
5
{
6
    /**
7
     * @var 缓存处理
0 ignored issues
show
Documentation Bug introduced by
The doc comment 缓存处理 at position 0 could not be parsed: Unknown type name '缓存处理' at position 0 in 缓存处理.
Loading history...
8
     */
9
    protected $cache;
10
11
    /**
12
     * Description:  执行GET请求操作
13
     * @author: JiaMeng <[email protected]>
14
     * Updater:
15
     * @param $url
16
     * @param array $params
17
     * @param array $headers
18
     * @return string
19
     */
20
    protected function get($url, $params = [], $headers = [])
21
    {
22
        return \tinymeng\tools\HttpRequest::httpGet($url, $params,$headers);
23
    }
24
25
    /**
26
     * Description:  执行POST请求操作
27
     * @author: JiaMeng <[email protected]>
28
     * Updater:
29
     * @param $url
30
     * @param array $params
31
     * @param array $headers
32
     * @return mixed
33
     */
34
    protected function post($url, $params = [], $headers = [])
35
    {
36
        $headers[] = 'Accept: application/json';//GitHub需要的header
37
        return \tinymeng\tools\HttpRequest::httpPost($url, $params,$headers);
38
    }
39
40
41
    public function setCache($cache)
42
    {
43
        $this->cache = $cache;
44
        return $this;
45
    }
46
47
    protected function getTokenFromCache($key)
48
    {
49
        return $this->cache ? $this->cache->get($key) : null;
50
    }
51
52
    protected function setTokenToCache($key, $token, $expires = 7200)
53
    {
54
        if ($this->cache) {
55
            $this->cache->set($key, $token, $expires);
56
        }
57
    }
58
59
}