CacheItem::isHit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
eloc 1
nc 1
nop 0
cc 1
crap 1
1
<?php
2
3
namespace Koded\Caching;
4
5
use Psr\Cache\CacheItemInterface;
6
7
abstract class CacheItem implements CacheItemInterface
8
{
9
    protected bool $isHit = false;
10
    private string $key;
11
    private mixed $value = null;
12
    private ?int $expiresAt;
13
14
    public function __construct($key, ?int $ttl)
15
    {
16
        $this->key = $key;
17
        $this->expiresAt = $ttl;
18
    }
19
20
    public function getKey(): string
21
    {
22
        return $this->key;
23 483
    }
24
25 483
    public function get()
26 483
    {
27 483
        return $this->value;
28
    }
29
30 237
    public function isHit(): bool
31
    {
32 237
        return $this->isHit;
33
    }
34
35
    public function set($value)
36 216
    {
37
        $this->value = $value;
38 216
        return $this;
39
    }
40
41
    public function expiresAfter($time): static
42 175
    {
43
        // The TTL is calculated in the cache client instance
44 175
        return $this->expiresAt($time);
45
    }
46
47
    public function expiresAt($expiration): static
48 258
    {
49
        $this->expiresAt = normalize_ttl($expiration ?? $this->expiresAt);
50 258
        if ($this->expiresAt < 1) {
51
            $this->isHit = false;
52 258
        }
53
        return $this;
54
    }
55
56 29
    /**
57
     * Returns expiration seconds for the cache item.
58
     * NULL is reserved for clients who do not support expiry
59 29
     * to implement some custom logic around the TTL.
60
     *
61
     * This method is not part of the PSR-6.
62
     *
63 57
     * @return int|null
64
     */
65 57
    public function getExpiresAt(): ?int
66
    {
67 57
        return $this->expiresAt;
68 28
    }
69
}
70