Passed
Push — master ( 0d9e84...07494d )
by Petr
02:10
created

Semaphore   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
c 1
b 0
f 1
dl 0
loc 71
ccs 34
cts 34
cp 1
rs 10
wmc 14

6 Methods

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