Completed
Push — master ( 953783...055045 )
by Dominik
02:02
created

ModelCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Cache;
6
7
use Chubbyphp\Model\ModelInterface;
8
9
final class ModelCache implements ModelCacheInterface
10
{
11
    /**
12
     * @var ModelInterface[]|array
13
     */
14
    private $cache = [];
15
16
    /**
17
     * @param ModelInterface $model
18
     *
19
     * @return ModelCacheInterface
20
     */
21
    public function set(ModelInterface $model): ModelCacheInterface
22
    {
23
        $this->cache[$model->getId()] = $model;
24
25
        return $this;
26
    }
27
28
    /**
29
     * @param string $id
30
     *
31
     * @return bool
32
     */
33
    public function has(string $id): bool
34
    {
35
        return array_key_exists($id, $this->cache);
36
    }
37
38
    /**
39
     * @param string $id
40
     *
41
     * @return ModelInterface
42
     *
43
     * @throws ModelNotFoundException
44
     */
45
    public function get(string $id)
46
    {
47
        if (!$this->has($id)) {
48
            throw ModelNotFoundException::fromId($id);
49
        }
50
51
        return $this->cache[$id];
52
    }
53
54
    /**
55
     * @param string $id
56
     *
57
     * @return ModelCacheInterface
58
     */
59
    public function remove(string $id): ModelCacheInterface
60
    {
61
        unset($this->cache[$id]);
62
63
        return $this;
64
    }
65
}
66