Passed
Push — master ( da7d9b...0d9e84 )
by Petr
07:09 queued 05:00
created

Dual::init()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
c 1
b 0
f 1
nc 5
nop 1
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
crap 2.024
rs 9.9
1
<?php
2
3
namespace kalanis\kw_cache\Files;
4
5
6
use kalanis\kw_cache\CacheException;
7
use kalanis\kw_cache\Interfaces\ICache;
8
use kalanis\kw_files\Access\CompositeAdapter;
9
use kalanis\kw_files\FilesException;
10
use kalanis\kw_files\Traits\TToString;
11
use kalanis\kw_paths\ArrayPath;
12
use kalanis\kw_paths\PathsException;
13
use kalanis\kw_paths\Stuff;
14
15
16
/**
17
 * Class Dual
18
 * @package kalanis\kw_cache\Files
19
 * Caching content by files - file as semaphore
20
 */
21
class Dual implements ICache
22
{
23
    use TToString;
24
25
    /** @var CompositeAdapter */
26
    protected $cacheLib = null;
27
    /** @var CompositeAdapter */
28
    protected $reloadLib = null;
29
    /** @var string[] */
30
    protected $cachePath = [];
31
    /** @var string[] */
32
    protected $reloadPath = [];
33
34 7
    public function __construct(CompositeAdapter $cacheLib, ?CompositeAdapter $reloadLib = null)
35
    {
36 7
        $this->cacheLib = $cacheLib;
37 7
        $this->reloadLib = $reloadLib ?? $cacheLib;
38 7
    }
39
40 6
    public function init(string $what): void
41
    {
42
        try {
43 6
            $arr = new ArrayPath();
44 6
            $arr->setString($what);
45 6
            $this->cachePath = array_merge(
46 6
                $arr->getArrayDirectory(),
47 6
                [$arr->getFileName() . ICache::EXT_CACHE]
48
            );;
49 6
            $this->reloadPath = array_merge(
50 6
                $arr->getArrayDirectory(),
51 6
                [$arr->getFileName() . ICache::EXT_RELOAD]
52
            );
53
            // @codeCoverageIgnoreStart
54
        } catch (PathsException $ex) {
55
            // just when it has problems with separating the path
56
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
57
        }
58
        // @codeCoverageIgnoreEnd
59 6
    }
60
61 4
    public function exists(): bool
62
    {
63
        try {
64 4
            return $this->cacheLib->exists($this->cachePath) && !$this->reloadLib->exists($this->reloadPath);
65 1
        } catch (FilesException | PathsException $ex) {
66 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
67
        }
68
    }
69
70 3
    public function set(string $content): bool
71
    {
72
        try {
73 3
            $result = $this->cacheLib->saveFile($this->cachePath, $content);
74 2
            if (false === $result) {
75 1
                return false;
76
            }
77
            // remove signal to save
78 1
            if ($this->reloadLib->exists($this->reloadPath)) {
79 1
                $this->reloadLib->deleteFile($this->reloadPath);
80
            }
81 1
            return true;
82 1
        } catch (FilesException | PathsException $ex) {
83 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
84
        }
85
    }
86
87 3
    public function get(): string
88
    {
89
        try {
90 3
            return $this->exists()
91 2
                ? $this->toString(Stuff::arrayToPath($this->cachePath), $this->cacheLib->readFile($this->cachePath))
92 2
                : ''
93
            ;
94 1
        } catch (FilesException | PathsException $ex) {
95 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
96
        }
97
    }
98
99 2
    public function clear(): void
100
    {
101
        try {
102 2
            $this->cacheLib->deleteFile($this->cachePath);
103 1
        } catch (FilesException | PathsException $ex) {
104 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
105
        }
106 1
    }
107
}
108