Passed
Pull Request — master (#74)
by Evgeniy
02:06
created

CacheItems::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache\Metadata;
6
7
use Psr\SimpleCache\CacheInterface;
8
9
/**
10
 * CacheItems store the metadata of each cache item.
11
 *
12
 * @internal
13
 */
14
final class CacheItems
15
{
16
    /**
17
     * @var array<string, CacheItem>
18
     */
19
    private array $items = [];
20
21
    /**
22
     * @param string $key
23
     * @param float $beta
24
     * @param CacheInterface $cache
25
     * @return mixed|null
26
     */
27 5
    public function getValue(string $key, float $beta, CacheInterface $cache)
28
    {
29 5
        if (isset($this->items[$key]) && !$this->items[$key]->expired($beta, $cache)) {
30 5
            return $this->items[$key]->value();
31
        }
32
33 2
        return null;
34
    }
35
36 41
    public function set(CacheItem $item): void
37
    {
38 41
        $key = $item->key();
39
40 41
        if (!isset($this->items[$key])) {
41 41
            $this->items[$key] = $item;
42 41
            return;
43
        }
44
45 2
        $this->items[$key]->update($item->value(), $item->expiry(), $item->dependency());
46 2
    }
47
48 13
    public function remove(string $key): void
49
    {
50 13
        if (isset($this->items[$key])) {
51 13
            unset($this->items[$key]);
52
        }
53 13
    }
54
55 48
    public function has(string $key): bool
56
    {
57 48
        return isset($this->items[$key]);
58
    }
59
}
60