Passed
Pull Request — master (#205)
by Wilmer
16:13
created

SchemaCache::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Cache;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Yiisoft\Cache\CacheKeyNormalizer;
9
use Yiisoft\Cache\Dependency\Dependency;
10
use Yiisoft\Cache\Dependency\TagDependency;
11
12
final class SchemaCache
13
{
14
    private CacheInterface $cache;
15
    private bool $enabled = true;
16
    private int $duration = 3600;
17
    private array $exclude = [];
18 2988
    private CacheKeyNormalizer $keyNormalizer;
19
20 2988
    public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
21 2988
    {
22 2988
        $this->cache = $cache;
23
        $this->keyNormalizer = $keyNormalizer;
24 18
    }
25
26 18
    public function delete($key): bool
27
    {
28
        return $this->cache->delete($key);
29 1613
    }
30
31 1613
    public function normalize($key): string
32
    {
33
        return $this->keyNormalizer->normalize($key);
34 1535
    }
35
36 1535
    public function get($key, $default = null)
37
    {
38
        return $this->cache->get($key, $default);
39 1595
    }
40
41 1595
    public function getDuration(): int
42
    {
43
        return $this->duration;
44 1613
    }
45
46 1613
    public function isExclude(string $value): bool
47
    {
48
        return in_array($value, $this->exclude, true);
49
    }
50
51
    public function invalidate(string $cacheTag): void
52
    {
53
        TagDependency::invalidate($this->cache, $cacheTag);
54
    }
55
56
    public function isEnabled(): bool
57
    {
58 39
        return $this->enabled;
59
    }
60 39
61 39
    public function set($key, $value, $ttl = null, Dependency $dependency = null): bool
62
    {
63
        return $this->cache->set($key, $value, $ttl, $dependency);
0 ignored issues
show
Unused Code introduced by
The call to Psr\SimpleCache\CacheInterface::set() has too many arguments starting with $dependency. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        return $this->cache->/** @scrutinizer ignore-call */ set($key, $value, $ttl, $dependency);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
64
    }
65
66
    /**
67
     * Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
68
     * specified must be enabled and {@see setEnable()} must be set true.
69
     *
70
     * @param bool $value
71
     *
72
     * {@see setduration()}
73
     * {@see setExclude()}
74
     */
75
    public function setEnable(bool $value): void
76
    {
77
        $this->enabled = $value;
78
    }
79
80
    /**
81
     * Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will
82
     * never expire.
83
     *
84
     * @param int $value
85
     *
86
     * {@see setEnable()}
87
     */
88
    public function setDuration(int $value): void
89
    {
90
        $this->duration = $value;
91
    }
92
93
    /**
94
     * List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema
95
     * prefix, if any. Do not quote the table names.
96
     *
97
     * @param array $value
98
     *
99
     * {@see setEnable()}
100
     */
101
    public function setExclude(array $value): void
102
    {
103
        $this->exclude = $value;
104
    }
105
}
106