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

MemoryLock   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 39
ccs 15
cts 16
cp 0.9375
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 6 2
A has() 0 3 1
A delete() 0 8 2
A create() 0 8 2
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