Passed
Pull Request — master (#82)
by Aleksei
02:10
created

SimpleCacheSchemaProvider::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 0
crap 2.032
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 5
    public function __construct(CacheInterface $cache)
17
    {
18 5
        $this->cache = $cache;
19 5
    }
20
21 3
    public function withConfig(array $config): self
22
    {
23 3
        $new = clone $this;
24 3
        $new->key = $config['key'] ?? self::DEFAULT_KEY;
25 3
        return $new;
26
    }
27
28 3
    public function read(?SchemaProviderInterface $nextProvider = null): ?array
29
    {
30 3
        $schema = $this->cache->get($this->key);
31
32 3
        if ($schema !== null || $nextProvider === null) {
33 2
            return $schema;
34
        }
35
36 1
        $schema = $nextProvider->read();
37 1
        if ($schema !== null) {
38 1
            $this->write($schema);
39
        }
40 1
        return $schema;
41
    }
42
43 2
    public function clear(): bool
44
    {
45 2
        $result = $this->cache->delete($this->key);
46 2
        if ($result === false) {
47
            throw new \RuntimeException('?');
48
        }
49 2
        return true;
50
    }
51
52 1
    private function write(array $schema): bool
53
    {
54 1
        return $this->cache->set($this->key, $schema);
55
    }
56
}
57