Completed
Push — master ( 0c66be...0b44f9 )
by Dominik
01:39
created

ArrayStorageCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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