Semaphore::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_cache\Storage;
4
5
6
use kalanis\kw_cache\CacheException;
7
use kalanis\kw_cache\Interfaces\ICache;
8
use kalanis\kw_paths\ArrayPath;
9
use kalanis\kw_paths\PathsException;
10
use kalanis\kw_paths\Stuff;
11
use kalanis\kw_semaphore\Interfaces\ISemaphore;
12
use kalanis\kw_semaphore\SemaphoreException;
13
use kalanis\kw_storage\Interfaces\IStorage;
14
use kalanis\kw_storage\StorageException;
15
16
17
/**
18
 * Class Semaphore
19
 * @package kalanis\kw_cache\Storage
20
 * Caching content in storage - semaphore for detection
21
 */
22
class Semaphore implements ICache
23
{
24
    protected IStorage $storage;
25
    protected ISemaphore $reloadSemaphore;
26
    /** @var string[] */
27
    protected array $cachePath = [ICache::EXT_CACHE];
28
29 7
    public function __construct(IStorage $cacheStorage, ISemaphore $reloadSemaphore)
30
    {
31 7
        $this->storage = $cacheStorage;
32 7
        $this->reloadSemaphore = $reloadSemaphore;
33 7
    }
34
35 5
    public function init(array $what): void
36
    {
37 5
        $arr = new ArrayPath();
38 5
        $arr->setArray($what);
39 5
        $this->cachePath = array_merge(
40 5
            $arr->getArrayDirectory(),
41 5
            [$arr->getFileName() . ICache::EXT_CACHE]
42
        );
43 5
    }
44
45 5
    public function exists(): bool
46
    {
47
        try {
48 5
            $cachePath = Stuff::arrayToPath($this->cachePath);
49 5
            return $this->storage->exists($cachePath) && !$this->reloadSemaphore->has();
50 1
        } catch (SemaphoreException | StorageException | PathsException $ex) {
51 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
52
        }
53
    }
54
55 4
    public function set(string $content): bool
56
    {
57
        try {
58 4
            $cachePath = Stuff::arrayToPath($this->cachePath);
59 4
            $result = $this->storage->write($cachePath, $content, null);
60 3
            if (false === $result) {
61 1
                return false;
62
            }
63
            // remove signal to save
64 2
            if ($this->reloadSemaphore->has()) {
65 2
                $this->reloadSemaphore->remove();
66
            }
67 2
        } catch (SemaphoreException | StorageException | PathsException $ex) {
68 2
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
69
        }
70 2
        return true;
71
    }
72
73 4
    public function get(): string
74
    {
75
        try {
76 4
            $cachePath = Stuff::arrayToPath($this->cachePath);
77 4
            return $this->exists() ? strval($this->storage->read($cachePath)) : '';
78 1
        } catch (StorageException | PathsException $ex) {
79 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
80
        }
81
    }
82
83 2
    public function clear(): void
84
    {
85
        try {
86 2
            $cachePath = Stuff::arrayToPath($this->cachePath);
87 2
            $this->storage->remove($cachePath);
88 1
        } catch (StorageException | PathsException $ex) {
89 1
            throw new CacheException($ex->getMessage(), $ex->getCode(), $ex);
90
        }
91 1
    }
92
}
93