Passed
Push — master ( ab102c...5f4e36 )
by Alexander
02:10
created

NullCache::setMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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