Passed
Push — master ( 75b0ea...bfe1c5 )
by Petr
08:08
created

MemoryLock::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
1
<?php
2
3
namespace kalanis\kw_locks\Methods;
4
5
6
use kalanis\kw_locks\Interfaces\ILock;
7
use kalanis\kw_locks\LockException;
8
9
10
/**
11
 * Class MemoryLock
12
 * @package kalanis\kw_locks\Methods
13
 * Lock some target
14
 * Uses low-level volume operations
15
 */
16
class MemoryLock implements ILock
17
{
18
    protected bool $lock = false;
19
20 1
    public function __destruct()
21
    {
22
        try {
23 1
            $this->delete();
24
            // @codeCoverageIgnoreStart
25
        } catch (LockException $ex) {
26
            // do nothing instead of
27
            // register_shutdown_function([$this, 'delete']);
28
        }
29
        // @codeCoverageIgnoreEnd
30 1
    }
31
32 1
    public function has(): bool
33
    {
34 1
        return $this->lock;
35
    }
36
37 1
    public function create(bool $force = false): bool
38
    {
39 1
        if ($this->has()) {
40 1
            return false;
41
        }
42
43 1
        $this->lock = true;
44 1
        return true;
45
    }
46
47 1
    public function delete(bool $force = false): bool
48
    {
49 1
        if (!$this->has()) {
50 1
            return true;
51
        }
52
53 1
        $this->lock = false;
54 1
        return true;
55
    }
56
}
57