CacheItem::isHit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCacheAdapter\CacheItemPool;
6
7
use Psr\Cache;
8
use RemotelyLiving\PHPCacheAdapter\Exceptions;
9
10
final class CacheItem implements Cache\CacheItemInterface, \Serializable
11
{
12
    private string $key;
13
14
    /**
15
     * @var mixed
16
     */
17
    private $value = null;
18
19
    private bool $isHit = false;
20
21
    private ?int $ttl = null;
22
23
    private function __construct()
24
    {
25
    }
26
27
    public static function create(string $key): self
28
    {
29
        $instance = new self();
30
        $instance->key = $key;
31
32
        return $instance;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function getKey(): string
39
    {
40
        return $this->key;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function get()
47
    {
48
        return $this->value;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function isHit(): bool
55
    {
56
        return $this->isHit;
57
    }
58
59
    public function setAsHit(): self
60
    {
61
        $this->isHit = true;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function set($value): self
70
    {
71
        $this->value = $value;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function expiresAt($expiration): self
80
    {
81
        if ($expiration === null) {
82
            $this->ttl = null;
83
            return $this;
84
        }
85
86
        if (!is_object($expiration) || !($expiration instanceof \DateTimeInterface)) {
87
            throw Exceptions\InvalidArgument::invalidTTL();
88
        }
89
90
        $this->ttl = max(0, $expiration->getTimestamp() - time());
91
92
        return $this;
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function expiresAfter($time): self
99
    {
100
        if ($time === null) {
101
            return $this;
102
        }
103
104
        if (is_int($time)) {
105
            $this->ttl = (int) $time;
106
            return $this;
107
        }
108
109
        if (is_object($time) && $time instanceof \DateTimeInterface) {
110
            return $this->expiresAt($time);
111
        }
112
113
        throw Exceptions\InvalidArgument::invalidTTL();
114
    }
115
116
    public function getTTL(): ?int
117
    {
118
        return $this->ttl;
119
    }
120
121
    public function unserialize($serialized, array $options = [])
122
    {
123
        $unserialized = \unserialize($serialized, $options);
124
        $this->key = (string) $unserialized['key'];
125
        $this->value = $unserialized['value'] ?? null;
126
        $this->ttl = $unserialized['ttl'] ?? null;
127
    }
128
129
    public function serialize(): string
130
    {
131
        return \serialize([
132
          'key' => $this->key,
133
          'value' => $this->value,
134
          'ttl' => $this->ttl
135
        ]);
136
    }
137
}
138