Semaphore::get()   A
last analyzed

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
    protected CompositeAdapter $lib;
28
    protected ISemaphore $reloadSemaphore;
29
    /** @var string[] */
30
    protected array $cachePath = [ICache::EXT_CACHE];
31
32 7
    public function __construct(CompositeAdapter $lib, ISemaphore $reloadSemaphore)
33
    {
34 7
        $this->lib = $lib;
35 7
        $this->reloadSemaphore = $reloadSemaphore;
36 7
    }
37
38 5
    public function init(array $what): void
39
    {
40 5
        $arr = new ArrayPath();
41 5
        $arr->setArray($what);
42 5
        $this->cachePath = array_merge(
43 5
            $arr->getArrayDirectory(),
44 5
            [$arr->getFileName() . ICache::EXT_CACHE]
45
        );
46 5
    }
47
48 5
    public function exists(): bool
49
    {
50
        try {
51 5
            return $this->lib->exists($this->cachePath) && !$this->reloadSemaphore->has();
52 1
        } catch (SemaphoreException | FilesException | PathsException $ex) {
53 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
54
        }
55
    }
56
57 4
    public function set(string $content): bool
58
    {
59
        try {
60 4
            $result = $this->lib->saveFile($this->cachePath, $content);
61 3
            if (false === $result) {
62 1
                return false;
63
            }
64
            // remove signal to save
65 2
            if ($this->reloadSemaphore->has()) {
66 2
                $this->reloadSemaphore->remove();
67
            }
68 2
        } catch (SemaphoreException | FilesException | PathsException $ex) {
69 2
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
70
        }
71 2
        return true;
72
    }
73
74 4
    public function get(): string
75
    {
76
        try {
77 4
            return $this->exists()
78 3
                ? $this->toString(Stuff::arrayToPath($this->cachePath), $this->lib->readFile($this->cachePath))
79 3
                : ''
80
            ;
81 1
        } catch (FilesException | PathsException $ex) {
82 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
83
        }
84
    }
85
86 2
    public function clear(): void
87
    {
88
        try {
89 2
            $this->lib->deleteFile($this->cachePath);
90 1
        } catch (FilesException | PathsException $ex) {
91 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
92
        }
93 1
    }
94
}
95