Passed
Push — master ( 0000af...b571e7 )
by Petr
07:35
created

Files::want()   A

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 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
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_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_paths\ArrayPath;
9
use kalanis\kw_paths\PathsException;
10
use kalanis\kw_semaphore\Interfaces\ISemaphore;
11
use kalanis\kw_semaphore\SemaphoreException;
12
13
14
/**
15
 * Class Files
16
 * @package kalanis\kw_semaphore\Semaphore
17
 * Data source for semaphore is files
18
 */
19
class Files implements ISemaphore
20
{
21
    /** @var string[] */
22
    protected $rootPath = [];
23
    /** @var CompositeAdapter */
24
    protected $lib = null;
25
26
    /**
27
     * @param CompositeAdapter $lib
28
     * @param string[] $rootPath
29
     */
30 8
    public function __construct(CompositeAdapter $lib, array $rootPath)
31
    {
32 8
        $libArr = new ArrayPath();
33 8
        $libArr->setArray($rootPath);
34 8
        $this->rootPath = array_merge(
35 8
            $libArr->getArrayDirectory(),
36 8
            [$libArr->getFileName() . static::EXT_SEMAPHORE]
37
        );
38 8
        $this->lib = $lib;
39 8
    }
40
41 4
    public function want(): bool
42
    {
43
        try {
44 4
            return $this->lib->saveFile($this->rootPath, static::TEXT_SEMAPHORE);
45 1
        } catch (FilesException | PathsException $ex) {
46 1
            throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex);
47
        }
48
    }
49
50 6
    public function has(): bool
51
    {
52
        try {
53 6
            return $this->lib->exists($this->rootPath);
54 1
        } catch (FilesException | PathsException $ex) {
55 1
            throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex);
56
        }
57
    }
58
59 4
    public function remove(): bool
60
    {
61
        try {
62 4
            return $this->lib->deleteFile($this->rootPath);
63 1
        } catch (FilesException | PathsException $ex) {
64 1
            throw new SemaphoreException($ex->getMessage(), $ex->getCode(), $ex);
65
        }
66
    }
67
}
68