Completed
Push — master ( 277825...31cee4 )
by Dominik
01:47
created

EntryCache   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 6 1
A has() 0 4 1
A get() 0 8 2
A remove() 0 6 1
A clear() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Cache;
6
7
final class EntryCache implements EntryCacheInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $cache = [];
13
14
    /**
15
     * @param string $id
16
     * @param array  $entry
17
     *
18
     * @return EntryCacheInterface
19
     */
20
    public function set(string $id, array $entry): EntryCacheInterface
21
    {
22
        $this->cache[$id] = $entry;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @param string $id
29
     *
30
     * @return bool
31
     */
32
    public function has(string $id): bool
33
    {
34
        return array_key_exists($id, $this->cache);
35
    }
36
37
    /**
38
     * @param string $id
39
     *
40
     * @return array
41
     *
42
     * @throws EntryNotFoundException
43
     */
44
    public function get(string $id)
45
    {
46
        if (!$this->has($id)) {
47
            throw EntryNotFoundException::fromId($id);
48
        }
49
50
        return $this->cache[$id];
51
    }
52
53
    /**
54
     * @param string $id
55
     *
56
     * @return EntryCacheInterface
57
     */
58
    public function remove(string $id): EntryCacheInterface
59
    {
60
        unset($this->cache[$id]);
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return EntryCacheInterface
67
     */
68
    public function clear(): EntryCacheInterface
69
    {
70
        $this->cache = [];
71
72
        return $this;
73
    }
74
}
75