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

SimpleCacheSchemaProvider::__construct()   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
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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