Passed
Push — master ( 15245f...c5030b )
by Wilmer
15:22 queued 22s
created

SchemaCache::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
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
10
final class SchemaCache
11
{
12
    private CacheInterface $cache;
13
    private bool $enabled = true;
14
    private int $duration = 3600;
15
    private array $exclude = [];
16
    private CacheKeyNormalizer $keyNormalizer;
17
18
    public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
19
    {
20
        $this->cache = $cache;
21
        $this->keyNormalizer = $keyNormalizer;
22
    }
23
24
    public function normalize($key): string
25
    {
26
        return $this->keyNormalizer->normalize($key);
27
    }
28
29
    public function getCache(): CacheInterface
30
    {
31
        return $this->cache;
32
    }
33
34
    public function getDuration(): int
35
    {
36
        return $this->duration;
37
    }
38
39
    public function isExclude(string $value): bool
40
    {
41
        return !in_array($value, $this->exclude, true);
42
    }
43
44
    public function isEnabled(): bool
45
    {
46
        return $this->enabled;
47
    }
48
49
    /**
50
     * Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
51
     * specified must be enabled and {@see setEnable()} must be set true.
52
     *
53
     * @param bool $value
54
     *
55
     * {@see setduration()}
56
     * {@see setExclude()}
57
     */
58
    public function setEnable(bool $value): void
59
    {
60
        $this->enabled = $value;
61
    }
62
63
    /**
64
     * Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will
65
     * never expire.
66
     *
67
     * @param int $value
68
     *
69
     * {@see setEnable()}
70
     */
71
    public function setDuration(int $value): void
72
    {
73
        $this->duration = $value;
74
    }
75
76
    /**
77
     * List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema
78
     * prefix, if any. Do not quote the table names.
79
     *
80
     * @param array $value
81
     *
82
     * {@see setEnable()}
83
     */
84
    public function setExclude(array $value): void
85
    {
86
        $this->exclude = $value;
87
    }
88
}
89