Storage::remove()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_semaphore\Semaphore;
4
5
6
use kalanis\kw_paths\Stuff;
7
use kalanis\kw_semaphore\Interfaces\ISemaphore;
8
use kalanis\kw_semaphore\SemaphoreException;
9
use kalanis\kw_storage\Interfaces\IStorage;
10
use kalanis\kw_storage\StorageException;
11
12
13
/**
14
 * Class Storage
15
 * @package kalanis\kw_semaphore\Semaphore
16
 * Data source for semaphore is storage
17
 */
18
class Storage implements ISemaphore
19
{
20
    protected string $rootPath = '';
21
    protected IStorage $storage;
22
23 8
    public function __construct(IStorage $storage, string $rootPath)
24
    {
25 8
        $this->rootPath = Stuff::removeEndingSlash($rootPath) . static::EXT_SEMAPHORE;
26 8
        $this->storage = $storage;
27 8
    }
28
29 4
    public function want(): bool
30
    {
31
        try {
32 4
            return $this->storage->write($this->rootPath, static::TEXT_SEMAPHORE);
33 1
        } catch (StorageException $ex) {
34 1
            throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex);
35
        }
36
    }
37
38 6
    public function has(): bool
39
    {
40
        try {
41 6
            return $this->storage->exists($this->rootPath);
42 1
        } catch (StorageException $ex) {
43 1
            throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex);
44
        }
45
    }
46
47 4
    public function remove(): bool
48
    {
49
        try {
50 4
            return $this->storage->remove($this->rootPath);
51 1
        } catch (StorageException $ex) {
52 1
            throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex);
53
        }
54
    }
55
}
56