Completed
Push — 6.0 ( d6c8d7...cb8a0b )
by liu
05:58
created

CacheItem::expiresAfter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\cache;
14
15
use DateInterval;
16
use DateTime;
17
use DateTimeInterface;
18
use Psr\Cache\CacheItemInterface;
19
use think\cache\exception\InvalidArgumentException;
0 ignored issues
show
Bug introduced by
The type think\cache\exception\InvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
22
 * CacheItem实现类
23
 */
24
class CacheItem implements CacheItemInterface
25
{
26
    /**
27
     * 缓存Key
28
     * @var string
29
     */
30
    protected $key;
31
32
    /**
33
     * 缓存内容
34
     * @var mixed
35
     */
36
    protected $value;
37
38
    /**
39
     * 过期时间
40
     * @var int|DateTimeInterface
41
     */
42
    protected $expire;
43
44
    /**
45
     * 缓存tag
46
     * @var string
47
     */
48
    protected $tag;
49
50
    /**
51
     * 缓存是否命中
52
     * @var bool
53
     */
54
    protected $isHit = false;
55
56
    public function __construct(string $key = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
57
    {
58
        $this->key = $key;
59
    }
60
61
    /**
62
     * 为此缓存项设置「键」
63
     * @access public
64
     * @param  string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
65
     * @return $this
66
     */
67
    public function setKey(string $key)
68
    {
69
        $this->key = $key;
70
        return $this;
71
    }
72
73
    /**
74
     * 返回当前缓存项的「键」
75
     * @access public
76
     * @return string
77
     */
78
    public function getKey()
79
    {
80
        return $this->key;
81
    }
82
83
    /**
84
     * 返回当前缓存项的有效期
85
     * @access public
86
     * @return DateTimeInterface|int|null
87
     */
88
    public function getExpire()
89
    {
90
        if ($this->expire instanceof DateTimeInterface) {
91
            return $this->expire;
92
        }
93
94
        return $this->expire ? $this->expire - time() : null;
95
    }
96
97
    /**
98
     * 获取缓存Tag
99
     * @access public
100
     * @return string
101
     */
102
    public function getTag()
103
    {
104
        return $this->tag;
105
    }
106
107
    /**
108
     * 凭借此缓存项的「键」从缓存系统里面取出缓存项
109
     * @access public
110
     * @return mixed
111
     */
112
    public function get()
113
    {
114
        return $this->value;
115
    }
116
117
    /**
118
     * 确认缓存项的检查是否命中
119
     * @access public
120
     * @return bool
121
     */
122
    public function isHit(): bool
123
    {
124
        return $this->isHit;
125
    }
126
127
    /**
128
     * 为此缓存项设置「值」
129
     * @access public
130
     * @param  mixed $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
131
     * @return $this
132
     */
133
    public function set($value)
134
    {
135
        $this->value = $value;
136
        $this->isHit = true;
137
        return $this;
138
    }
139
140
    /**
141
     * 为此缓存项设置所属标签
142
     * @access public
143
     * @param  string $tag
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
144
     * @return $this
145
     */
146
    public function tag(string $tag = null)
147
    {
148
        $this->tag = $tag;
149
        return $this;
150
    }
151
152
    /**
153
     * 设置缓存项的有效期
154
     * @access public
155
     * @param  mixed $expire
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
156
     * @return $this
157
     */
158
    public function expire($expire)
159
    {
160
        if (is_null($expire)) {
161
            $this->expire = null;
162
        } elseif (is_numeric($expire) || $expire instanceof DateInterval) {
163
            $this->expiresAfter($expire);
164
        } elseif ($expire instanceof DateTimeInterface) {
165
            $this->expire = $expire;
166
        } else {
167
            throw new InvalidArgumentException('not support datetime');
168
        }
169
170
        return $this;
171
    }
172
173
    /**
174
     * 设置缓存项的准确过期时间点
175
     * @access public
176
     * @param  DateTimeInterface $expiration
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
177
     * @return $this
178
     */
179
    public function expiresAt($expiration)
180
    {
181
        if ($expiration instanceof DateTimeInterface) {
0 ignored issues
show
introduced by
$expiration is always a sub-type of DateTimeInterface.
Loading history...
182
            $this->expire = $expiration;
183
        } else {
184
            throw new InvalidArgumentException('not support datetime');
185
        }
186
187
        return $this;
188
    }
189
190
    /**
191
     * 设置缓存项的过期时间
192
     * @access public
193
     * @param int|DateInterval $timeInterval
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
194
     * @return $this
195
     * @throws InvalidArgumentException
196
     */
197
    public function expiresAfter($timeInterval)
198
    {
199
        if ($timeInterval instanceof DateInterval) {
200
            $this->expire = (int) DateTime::createFromFormat('U', (string) time())->add($timeInterval)->format('U');
201
        } elseif (is_numeric($timeInterval)) {
0 ignored issues
show
introduced by
The condition is_numeric($timeInterval) is always true.
Loading history...
202
            $this->expire = $timeInterval + time();
203
        } else {
204
            throw new InvalidArgumentException('not support datetime');
205
        }
206
207
        return $this;
208
    }
209
210
}
211