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

Semaphore::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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