Passed
Pull Request — develop (#2)
by Andreas
03:58 queued 01:12
created

DummyCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 83
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1
ccs 20
cts 20
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getKey() 0 4 1
A has() 0 4 1
A get() 0 8 2
A store() 0 4 1
A remove() 0 11 2
1
<?php
2
3
namespace Wambo\Catalog\Cache;
4
5
use Wambo\Core\Module\Exception\InvalidArgumentException;
6
use Wambo\Core\ValueObject\ValueObjectInterface;
7
use Wambo\Core\ValueObject\ValueObjectTrait;
8
9
/**
10
 * Class DummyCache
11
 *
12
 * @package Wambo\Catalog\Cache
13
 */
14
class DummyCache implements CacheInterface
15
{
16
    private $storage;
17
18
    /**
19
     * Creates a new DummyCache instance.
20
     */
21 17
    public function __construct()
22
    {
23 17
        $this->storage = array();
24 17
    }
25
26
    /**
27
     * Get a cache key for the given element name and tags.
28
     *
29
     * @param string    $elementName A name of the element that you want to cache
30
     * @param \string[] $tags        A list of tags that help to categorize the element you want to cache
31
     *
32
     * @return CacheKey
33
     */
34 17
    public function getKey(string $elementName, string ...$tags): CacheKey
35
    {
36 17
        return new CacheKey($elementName, ...$tags);
37
    }
38
39
    /**
40
     * Get a flag indicating if the the cache has an element with given cache key
41
     *
42
     * @param CacheKey $cacheKey The identifier for the cached value you want to retrieve
43
     *
44
     * @return bool
45
     */
46 6
    public function has(CacheKey $cacheKey): bool
47
    {
48 6
        return array_key_exists($cacheKey->getValue(), $this->storage);
49
    }
50
51
    /**
52
     * Get the data stored under the given cache key.
53
     *
54
     * @param CacheKey $cacheKey The identifier for the cached value you want to retrieve
55
     *
56
     * @return array|null The data stored under the given cache key; null if the cache does not exist.
57
     */
58 3
    public function get(CacheKey $cacheKey)
59
    {
60 3
        if ($this->has($cacheKey) === false) {
61 1
            return null;
62
        }
63
64 2
        return $this->storage[$cacheKey->getValue()];
65
    }
66
67
    /**
68
     * Store an array under the given cache key.s
69
     *
70
     * @param CacheKey $cacheKey The identifier for the cached value you want to retrieve
71
     * @param array    $value    An array with data
72
     */
73 3
    public function store(CacheKey $cacheKey, array $value)
74
    {
75 3
        $this->storage[$cacheKey->getValue()] = $value;
76 3
    }
77
78
    /**
79
     * Remove the data stored under the given cache key
80
     *
81
     * @param CacheKey $cacheKey The identifier for the cached value you want to retrieve
82
     *
83
     * @return array|null The data that was stored under the given key; null if the key does not exist.
84
     */
85 2
    public function remove(CacheKey $cacheKey)
86
    {
87 2
        if ($this->has($cacheKey) === false) {
88 1
            return null;
89
        }
90
91 1
        $value = $this->get($cacheKey);
92 1
        unset($this->storage[$cacheKey->getValue()]);
93
94 1
        return $value;
95
    }
96
}