Passed
Push — master ( bf075b...6deca2 )
by Petr
07:21
created

FilesLock::setKey()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 8
ccs 3
cts 5
cp 0.6
crap 3.576
rs 10
1
<?php
2
3
namespace kalanis\kw_locks\Methods;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_locks\Interfaces\IKLTranslations;
9
use kalanis\kw_locks\Interfaces\IPassedKey;
10
use kalanis\kw_locks\LockException;
11
use kalanis\kw_locks\Traits\TLang;
12
use kalanis\kw_paths\ArrayPath;
13
use kalanis\kw_paths\PathsException;
14
15
16
/**
17
 * Class FilesLock
18
 * @package kalanis\kw_locks\Methods
19
 */
20
class FilesLock implements IPassedKey
21
{
22
    use TLang;
23
24
    /** @var ArrayPath */
25
    protected $arrPt = null;
26
    /** @var CompositeAdapter */
27
    protected $files = null;
28
    /** @var string[] */
29
    protected $specialKey = [];
30
    /** @var string */
31
    protected $checkContent = '';
32
33 5
    public function __construct(CompositeAdapter $files, ?IKLTranslations $lang = null)
34
    {
35 5
        $this->arrPt = new ArrayPath();
36 5
        $this->files = $files;
37 5
        $this->setKlLang($lang);
38 5
    }
39
40 5
    public function __destruct()
41
    {
42
        try {
43 5
            $this->delete();
44 4
        } catch (LockException $ex) {
45
            // do nothing instead of
46
            // register_shutdown_function([$this, 'delete']);
47
        }
48 5
    }
49
50 5
    public function setKey(string $key, string $checkContent = ''): void
51
    {
52
        try {
53 5
            $this->specialKey = $this->arrPt->setString($key)->getArray();
54 5
            $this->checkContent = empty($checkContent) ? strval(getmypid()) : $checkContent ;
55
            // @codeCoverageIgnoreStart
56
        } catch (PathsException $ex) {
57
            throw new LockException($this->getKlLang()->iklCannotUsePath($key), $ex->getCode(), $ex);
58
        }
59
        // @codeCoverageIgnoreEnd
60 5
    }
61
62 5
    public function has(): bool
63
    {
64
        try {
65 5
            if (!$this->files->exists($this->specialKey)) {
66 2
                return false;
67
            }
68 2
            if ($this->checkContent == strval($this->files->readFile($this->specialKey))) {
69 2
                return true;
70
            }
71 1
            throw new LockException($this->getKlLang()->iklLockedByOther());
72 4
        } catch (FilesException | PathsException $ex) {
73 3
            throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex);
74
        }
75
    }
76
77 3
    public function create(bool $force = false): bool
78
    {
79 3
        if (!$force && $this->has()) {
80 2
            return false;
81
        }
82
        try {
83 3
            $result = $this->files->saveFile($this->specialKey, $this->checkContent);
84 2
            return $result;
85 1
        } catch (FilesException | PathsException $ex) {
86 1
            throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex);
87
        }
88
    }
89
90 5
    public function delete(bool $force = false): bool
91
    {
92 5
        if (!$force && !$this->has()) {
93 1
            return true;
94
        }
95
        try {
96 2
            return $this->files->deleteFile($this->specialKey);
97 1
        } catch (FilesException | PathsException $ex) {
98 1
            throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex);
99
        }
100
    }
101
}
102