Passed
Pull Request — master (#38)
by Aleksei
13:38
created

SimpleCacheSchemaProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A write() 0 3 1
A __construct() 0 3 1
A withConfig() 0 5 1
A isWritable() 0 3 1
A isReadable() 0 3 1
A read() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Provider;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;
9
10
final class SimpleCacheSchemaProvider implements SchemaProviderInterface
11
{
12
    public const DEFAULT_KEY = 'Cycle-ORM-Schema';
13
    private CacheInterface $cache;
14
    private string $key = self::DEFAULT_KEY;
15
16
    public function __construct(CacheInterface $cache)
17
    {
18
        $this->cache = $cache;
19
    }
20
21
    public function withConfig(array $config): SchemaProviderInterface
22
    {
23
        $clone = clone $this;
24
        $clone->key = $config['key'] ?? self::DEFAULT_KEY;
25
        return $clone;
26
    }
27
28
    public function read(): ?array
29
    {
30
        return $this->cache->get($this->key);
31
    }
32
33
    public function write($schema): bool
34
    {
35
        return $this->cache->set($this->key, $schema);
36
    }
37
38
    public function clear(): bool
39
    {
40
        return $this->cache->delete($this->key);
41
    }
42
    public function isWritable(): bool
43
    {
44
        return true;
45
    }
46
    public function isReadable(): bool
47
    {
48
        return true;
49
    }
50
}
51