Passed
Push — develop ( 9b8c4e...79e7ae )
by Andrea Marco
03:34 queued 14s
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
        /** @phpstan-ignore argument.type */
77 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

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