Passed
Push — feature/second-release ( 035066...5651c9 )
by Andrea Marco
02:56
created

CacheKey::sear()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Capsules;
6
7
use Closure;
8
use DateInterval;
9
use DateTimeInterface;
10
use Illuminate\Contracts\Cache\Lock;
11
use Illuminate\Support\Facades\Cache;
12
13
/**
14
 * The key dealing with the Laravel cache.
15
 */
16
final class CacheKey
17
{
18
    /**
19
     * Instantiate the class.
20
     */
21 18
    public function __construct(private readonly string $key) {}
22
23
    /**
24
     * Determine whether the key exists in the cache.
25
     */
26 1
    public function exists(): bool
27
    {
28 1
        return Cache::has($this->key);
29
    }
30
31
    /**
32
     * Determine whether the key doesn't exist in the cache.
33
     */
34 1
    public function missing(): bool
35
    {
36 1
        return Cache::missing($this->key);
37
    }
38
39
    /**
40
     * Determine whether the key is present and not null.
41
     */
42 1
    public function hasValue(): bool
43
    {
44 1
        return Cache::has($this->key);
45
    }
46
47
    /**
48
     * Retrieve the value of the key from the cache.
49
     */
50 1
    public function get(mixed $default = null): mixed
51
    {
52 1
        return Cache::get($this->key, $default);
53
    }
54
55
    /**
56
     * Retrieve the value of the key from the cache and delete it.
57
     */
58 1
    public function pull(mixed $default = null): mixed
59
    {
60 1
        return Cache::pull($this->key, $default);
61
    }
62
63
    /**
64
     * Store the value of the key in the cache.
65
     */
66 1
    public function put(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool
67
    {
68 1
        return Cache::put($this->key, $value, $ttl);
69
    }
70
71
    /**
72
     * Store the value of the key in the cache.
73
     */
74 1
    public function set(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool
75
    {
76 1
        return Cache::set($this->key, $value, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl can also be of type DateTimeInterface; however, parameter $ttl of Illuminate\Support\Facades\Cache::set() does only seem to accept DateInterval|integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return Cache::set($this->key, $value, /** @scrutinizer ignore-type */ $ttl);
Loading history...
77
    }
78
79
    /**
80
     * Store the value of the key in the cache if the key does not exist.
81
     */
82 1
    public function add(mixed $value, DateTimeInterface|DateInterval|int|null $ttl = null): bool
83
    {
84 1
        return Cache::add($this->key, $value, $ttl);
85
    }
86
87
    /**
88
     * Increment the value of the key in the cache.
89
     */
90 1
    public function increment(float|int $value = 1): float|int|bool
91
    {
92
        /** @var float|int|bool */
93 1
        return Cache::increment($this->key, $value);
94
    }
95
96
    /**
97
     * Decrement the value of the key in the cache.
98
     */
99 1
    public function decrement(float|int $value = 1): float|int|bool
100
    {
101
        /** @var float|int|bool */
102 1
        return Cache::decrement($this->key, $value);
103
    }
104
105
    /**
106
     * Store the value of the key in the cache indefinitely.
107
     */
108 1
    public function forever(mixed $value): bool
109
    {
110 1
        return Cache::forever($this->key, $value);
111
    }
112
113
    /**
114
     * Retrieve or store the value of the key.
115
     */
116 1
    public function remember(Closure|DateTimeInterface|DateInterval|int|null $ttl, Closure $callback): mixed
117
    {
118 1
        return Cache::remember($this->key, $ttl, $callback);
119
    }
120
121
    /**
122
     * Retrieve or store indefinitely the value of the key.
123
     */
124 1
    public function rememberForever(Closure $callback): mixed
125
    {
126 1
        return Cache::rememberForever($this->key, $callback);
127
    }
128
129
    /**
130
     * Retrieve or store indefinitely the value of the key.
131
     */
132 1
    public function sear(Closure $callback): mixed
133
    {
134 1
        return Cache::sear($this->key, $callback);
135
    }
136
137
    /**
138
     * Remove the key from the cache.
139
     */
140 1
    public function forget(): bool
141
    {
142 1
        return Cache::forget($this->key);
143
    }
144
145
    /**
146
     * Delete the key from the cache.
147
     */
148 1
    public function delete(): bool
149
    {
150 1
        return Cache::delete($this->key);
151
    }
152
153
    /**
154
     * Retrieve the lock instance.
155
     */
156 1
    public function lock(int $seconds = 0, ?string $owner = null): Lock
157
    {
158 1
        return Cache::lock($this->key, $seconds, $owner);
159
    }
160
161
    /**
162
     * Restore the lock instance using the owner identifier.
163
     */
164 1
    public function restoreLock(string $owner): Lock
165
    {
166 1
        return Cache::restoreLock($this->key, $owner);
167
    }
168
}
169