Passed
Push — master ( 0080cb...86f92d )
by Alexander
01:27
created

NullCache::validateKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 1
crap 3.3332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache;
6
7
use Yiisoft\Cache\Dependency\Dependency;
8
use Yiisoft\Cache\Exception\InvalidArgumentException;
9
10
/**
11
 * NullCache does not cache anything reporting success for all methods calls.
12
 *
13
 * By replacing it with some other cache component, one can quickly switch from
14
 * non-caching mode to caching mode.
15
 * @phan-file-suppress PhanUnusedPublicFinalMethodParameter
16
 */
17
final class NullCache implements CacheInterface
18
{
19 1
    public function add($key, $value, $ttl = 0, Dependency $dependency = null): bool
20
    {
21 1
        $this->validateKey($key);
22 1
        return true;
23
    }
24
25 1
    public function deleteMultiple($keys): bool
26
    {
27 1
        $keys = $this->iterableToArray($keys);
28 1
        $this->validateKeys($keys);
29 1
        return true;
30
    }
31
32 1
    public function set($key, $value, $ttl = null, Dependency $dependency = null): bool
33
    {
34 1
        $this->validateKey($key);
35 1
        return true;
36
    }
37
38 1
    public function get($key, $default = null)
39
    {
40 1
        $this->validateKey($key);
41 1
        return $default;
42
    }
43
44 1
    public function getMultiple($keys, $default = null): iterable
45
    {
46 1
        $keys = $this->iterableToArray($keys);
47 1
        $this->validateKeys($keys);
48 1
        return array_fill_keys($keys, $default);
49
    }
50
51 1
    public function setMultiple($values, $ttl = null, Dependency $dependency = null): bool
52
    {
53 1
        $values = $this->iterableToArray($values);
54 1
        $this->validateKeysOfValues($values);
55 1
        return true;
56
    }
57
58 1
    public function addMultiple(array $values, $ttl = null, Dependency $dependency = null): bool
59
    {
60 1
        $values = $this->iterableToArray($values);
61 1
        $this->validateKeysOfValues($values);
62 1
        return true;
63
    }
64
65 1
    public function getOrSet($key, callable $callable, $ttl = null, Dependency $dependency = null)
66
    {
67 1
        $this->validateKey($key);
68 1
        return $callable($this);
69
    }
70
71 1
    public function delete($key): bool
72
    {
73 1
        $this->validateKey($key);
74 1
        return true;
75
    }
76
77 1
    public function clear(): bool
78
    {
79 1
        return true;
80
    }
81
82 1
    public function has($key): bool
83
    {
84 1
        $this->validateKey($key);
85 1
        return false;
86
    }
87
88
    public function enableKeyNormalization(): void
89
    {
90
        // do nothing
91
    }
92
93
    public function disableKeyNormalization(): void
94
    {
95
        // do nothing
96
    }
97
98
    public function setKeyPrefix(string $keyPrefix): void
99
    {
100
        // do nothing
101
    }
102
103 4
    private function iterableToArray(iterable $iterable): array
104
    {
105 4
        return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable;
106
    }
107
108
    /**
109
     * @param $key
110
     */
111 10
    private function validateKey($key): void
112
    {
113 10
        if (!\is_string($key) || strpbrk($key, '{}()/\@:')) {
114
            throw new InvalidArgumentException('Invalid key value.');
115
        }
116
    }
117
118
    /**
119
     * @param array $keys
120
     */
121 4
    private function validateKeys(array $keys): void
122
    {
123 4
        foreach ($keys as $key) {
124 4
            $this->validateKey($key);
125
        }
126
    }
127
128
    /**
129
     * @param array $values
130
     */
131 2
    private function validateKeysOfValues(array $values): void
132
    {
133 2
        $keys = array_map('strval', array_keys($values));
134 2
        $this->validateKeys($keys);
135
    }
136
}
137