ArrayStorageCache::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
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 Chubbyphp\Model\StorageCache;
6
7
final class ArrayStorageCache implements StorageCacheInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $cache = [];
13
14
    /**
15
     * @param array $cache
16
     */
17 7
    public function __construct(array $cache = [])
18
    {
19 7
        $this->cache = $cache;
20 7
    }
21
22
    /**
23
     * @param string $id
24
     * @param array  $entry
25
     *
26
     * @return StorageCacheInterface
27
     */
28 1
    public function set(string $id, array $entry): StorageCacheInterface
29
    {
30 1
        $this->cache[$id] = $entry;
31
32 1
        return $this;
33
    }
34
35
    /**
36
     * @param string $id
37
     *
38
     * @return bool
39
     */
40 2
    public function has(string $id): bool
41
    {
42 2
        return array_key_exists($id, $this->cache);
43
    }
44
45
    /**
46
     * @param string $id
47
     *
48
     * @return array
49
     *
50
     * @throws EntryNotFoundException
51
     */
52 2
    public function get(string $id)
53
    {
54 2
        if (!$this->has($id)) {
55 1
            throw EntryNotFoundException::fromId($id);
56
        }
57
58 1
        return $this->cache[$id];
59
    }
60
61
    /**
62
     * @param string $id
63
     *
64
     * @return StorageCacheInterface
65
     */
66 1
    public function remove(string $id): StorageCacheInterface
67
    {
68 1
        unset($this->cache[$id]);
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * @return StorageCacheInterface
75
     */
76 1
    public function clear(): StorageCacheInterface
77
    {
78 1
        $this->cache = [];
79
80 1
        return $this;
81
    }
82
}
83