Completed
Push — master ( be5d10...3892c2 )
by Sergey
07:31
created

RuntimeCache::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1.125
1
<?php
2
3
namespace Isswp101\Persimmon\Cache;
4
5
use Isswp101\Persimmon\Model;
6
7
class RuntimeCache
8
{
9
    /**
10
     * Cache.
11
     *
12
     * @var array [
13
     *   'instance' => Model,
14
     *   'attributes' => []
15
     * ]
16
     */
17
    private $cache = [];
18
19
    /**
20
     * Return true if cache contains this key.
21
     *
22
     * @param mixed $key
23
     * @return bool
24
     */
25 11
    public function has($key)
26
    {
27 11
        return array_key_exists($key, $this->cache);
28
    }
29
30
    /**
31
     * Return instance from cache.
32
     *
33
     * @param mixed $key
34
     * @return Model
35
     */
36 5
    public function get($key)
37
    {
38 5
        return $this->has($key) ? $this->cache[$key]['instance'] : null;
39
    }
40
41
    /**
42
     * Return all cache keys.
43
     *
44
     * @return array
45
     */
46
    public function keys()
47
    {
48
        return array_keys($this->cache);
49
    }
50
51
    /**
52
     * Put instance to cache.
53
     *
54
     * @param mixed $key
55
     * @param Model $instance
56
     * @param array $attributes
57
     * @return Model
58
     */
59 5
    public function put($key, Model $instance, array $attributes = ['*'])
60
    {
61 5
        if ($attributes != ['*'] && $this->has($key)) {
62
            $instance = Model::merge($this->cache[$key]['instance'], $instance, $attributes);
63
            $attributes = array_merge($this->cache[$key]['attributes'], $attributes);
64
        }
65
66 5
        $this->cache[$key] = [
67 5
            'instance' => $instance,
68
            'attributes' => $attributes
69 5
        ];
70
71 5
        return $instance;
72
    }
73
74
    /**
75
     * Return true if cache has already given attributes.
76
     *
77
     * @param mixed $key
78
     * @param array $attributes
79
     * @return bool
80
     */
81 11
    public function containsAttributes($key, array $attributes = ['*'])
82
    {
83 11
        return empty($this->getNotCachedAttributes($key, $attributes));
84
    }
85
86
    /**
87
     * Return the difference between given attributes and attributes which are already cached.
88
     *
89
     * @param mixed $key
90
     * @param array $attributes
91
     * @return array
92
     */
93 11
    public function getNotCachedAttributes($key, array $attributes = ['*'])
94
    {
95 11
        if (!$this->has($key)) {
96 6
            return $attributes;
97
        }
98 6
        $cachedAttributes = $this->cache[$key]['attributes'];
99 6
        return $cachedAttributes == ['*'] ? [] : array_diff($attributes, $cachedAttributes);
100
    }
101
102
    /**
103
     * Remove an item from the cache by key.
104
     *
105
     * @param mixed $key
106
     * @return $this
107
     */
108 3
    public function forget($key)
109
    {
110 3
        unset($this->cache[$key]);
111 3
        return $this;
112
    }
113
}
114