Passed
Pull Request — master (#74)
by Evgeniy
01:54
created

CacheItems::has()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
     *
26
     * @return mixed|null
27
     */
28 5
    public function getValue(string $key, float $beta, CacheInterface $cache)
29
    {
30 5
        if (isset($this->items[$key]) && !$this->items[$key]->expired($beta, $cache)) {
31 5
            return $this->items[$key]->value();
32
        }
33
34 2
        return null;
35
    }
36
37 41
    public function set(CacheItem $item): void
38
    {
39 41
        $key = $item->key();
40
41 41
        if (!isset($this->items[$key])) {
42 41
            $this->items[$key] = $item;
43 41
            return;
44
        }
45
46 2
        $this->items[$key]->update($item->value(), $item->expiry(), $item->dependency());
47 2
    }
48
49 13
    public function remove(string $key): void
50
    {
51 13
        if (isset($this->items[$key])) {
52 13
            unset($this->items[$key]);
53
        }
54 13
    }
55
56 48
    public function has(string $key): bool
57
    {
58 48
        return isset($this->items[$key]);
59
    }
60
}
61