Total Complexity | 9 |
Total Lines | 78 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | abstract class CacheItem implements CacheItemInterface |
||
9 | { |
||
10 | /** @var bool */ |
||
11 | protected $isHit = false; |
||
12 | |||
13 | /** @var string Expects a valid cache key name */ |
||
14 | private $key; |
||
15 | |||
16 | /** @var mixed */ |
||
17 | private $value; |
||
18 | |||
19 | /** @var int Number of seconds for the expiration time */ |
||
20 | private $expiresAt; |
||
21 | |||
22 | |||
23 | 483 | public function __construct($key, ?int $ttl) |
|
24 | { |
||
25 | 483 | $this->key = $key; |
|
26 | 483 | $this->expiresAt = $ttl; |
|
27 | 483 | } |
|
28 | |||
29 | |||
30 | 237 | public function getKey(): string |
|
31 | { |
||
32 | 237 | return $this->key; |
|
33 | } |
||
34 | |||
35 | |||
36 | 216 | public function get() |
|
37 | { |
||
38 | 216 | return $this->value; |
|
39 | } |
||
40 | |||
41 | |||
42 | 175 | public function isHit(): bool |
|
45 | } |
||
46 | |||
47 | |||
48 | 258 | public function set($value) |
|
49 | { |
||
50 | 258 | $this->value = $value; |
|
51 | |||
52 | 258 | return $this; |
|
53 | } |
||
54 | |||
55 | |||
56 | 29 | public function expiresAfter($time) |
|
57 | { |
||
58 | // The TTL is calculated in the cache client instance |
||
59 | 29 | return $this->expiresAt($time); |
|
60 | } |
||
61 | |||
62 | |||
63 | 57 | public function expiresAt($expiration) |
|
64 | { |
||
65 | 57 | $this->expiresAt = normalize_ttl($expiration ?? $this->expiresAt); |
|
66 | |||
67 | 57 | if ($this->expiresAt < 1) { |
|
68 | 28 | $this->isHit = false; |
|
69 | } |
||
70 | |||
71 | 57 | return $this; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Returns expiration seconds for the cache item. |
||
76 | * NULL is reserved for clients who do not support expiry |
||
77 | * to implement some custom logic around the TTL. |
||
78 | * |
||
79 | * This method is not part of the PSR-6. |
||
80 | * |
||
81 | * @return int|null |
||
82 | */ |
||
83 | 251 | public function getExpiresAt(): ?int |
|
86 | } |
||
87 | } |
||
88 |