Passed
Push — master ( c5bf56...fcda2a )
by
unknown
09:05
created

CacheMap::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Butler\Graphql;
4
5
use Countable;
6
use leinonen\DataLoader\CacheMapInterface;
7
8
class CacheMap implements CacheMapInterface, Countable
9
{
10
    private $cache = [];
11
12 12
    public function get($key)
13
    {
14 12
        $key = $this->serializeKey($key);
15
16 12
        return array_key_exists($key, $this->cache)
17 3
            ? $this->cache[$key]
18 12
            : false;
19
    }
20
21 12
    public function set($key, $value)
22
    {
23 12
        $this->cache[self::serializeKey($key)] = $value;
24 12
    }
25
26 1
    public function delete($key)
27
    {
28 1
        unset($this->cache[self::serializeKey($key)]);
29 1
    }
30
31 1
    public function clear()
32
    {
33 1
        $this->cache = [];
34 1
    }
35
36 1
    public function count()
37
    {
38 1
        return count($this->cache);
39
    }
40
41 12
    private static function serializeKey($key)
42
    {
43 12
        if (is_object($key)) {
44 1
            return spl_object_hash($key);
45 12
        } elseif (is_array($key)) {
46 1
            return md5(json_encode($key));
47
        }
48 12
        return (string) $key;
49
    }
50
}
51