Dual   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 16
c 1
b 0
f 0
dl 0
loc 43
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 11 3
A exists() 0 3 2
A get() 0 3 2
A init() 0 4 1
A clear() 0 3 1
A __construct() 0 4 2
1
<?php
2
3
namespace kalanis\kw_cache\Cache;
4
5
6
use kalanis\kw_cache\Interfaces\ICache;
7
8
9
/**
10
 * Class Dual
11
 * @package kalanis\kw_cache\Cache
12
 * Caching content in any cache - another cache as semaphore for detection
13
 */
14
class Dual implements ICache
15
{
16
    protected ICache $storageCache;
17
    protected ICache $reloadCache;
18
19 4
    public function __construct(ICache $storageCache, ?ICache $reloadCache = null)
20
    {
21 4
        $this->storageCache = $storageCache;
22 4
        $this->reloadCache = $reloadCache ?: $storageCache;
23 4
    }
24
25 2
    public function init(array $what): void
26
    {
27 2
        $this->storageCache->init($what);
28 2
        $this->reloadCache->init($what);
29 2
    }
30
31 2
    public function exists(): bool
32
    {
33 2
        return $this->storageCache->exists() && !$this->reloadCache->exists();
34
    }
35
36 3
    public function set(string $content): bool
37
    {
38 3
        $result = $this->storageCache->set($content);
39 3
        if (false === $result) {
40 1
            return false;
41
        }
42
        // remove signal to save
43 2
        if ($this->reloadCache->exists()) {
44 2
            $this->reloadCache->clear();
45
        }
46 1
        return true;
47
    }
48
49 2
    public function get(): string
50
    {
51 2
        return $this->exists() ? $this->storageCache->get() : '' ;
52
    }
53
54 1
    public function clear(): void
55
    {
56 1
        $this->storageCache->clear();
57 1
    }
58
}
59