Storage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 6 2
A __construct() 0 4 1
A want() 0 6 2
A remove() 0 6 2
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