LockAdapterMutex::isLocked()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Metabor\Semaphore;
4
5
use MetaborStd\Semaphore\LockAdapterInterface;
6
use MetaborStd\Semaphore\MutexInterface;
7
8
class LockAdapterMutex implements MutexInterface
9
{
10
    /**
11
     * @var LockAdapterInterface
12
     */
13
    private $lockAdapter;
14
15
    /**
16
     * @var string
17
     */
18
    private $resourceName;
19
20
    /**
21
     * @var bool
22
     */
23
    private $acquired = false;
24
25
    /**
26
     * @param LockAdapterInterface $lockAdapter
27
     * @param string               $resourceName
28
     */
29
    public function __construct(LockAdapterInterface $lockAdapter, $resourceName)
30
    {
31
        $this->lockAdapter = $lockAdapter;
32
        $this->resourceName = $resourceName;
33
    }
34
35
    /**
36
     * @see \MetaborStd\Semaphore\MutexInterface::releaseLock()
37
     */
38
    public function releaseLock()
39
    {
40
        if ($this->acquired) {
41
            $result = $this->lockAdapter->releaseLock($this->resourceName);
42
            if ($result) {
43
                $this->acquired = false;
44
            }
45
46
            return $result;
47
        } else {
48
            return false;
49
        }
50
    }
51
52
    /**
53
     * @see \MetaborStd\Semaphore\MutexInterface::isAcquired()
54
     */
55
    public function isAcquired()
56
    {
57
        return $this->acquired;
58
    }
59
60
    /**
61
     * @see \MetaborStd\Semaphore\MutexInterface::acquireLock()
62
     */
63
    public function acquireLock()
64
    {
65
        if (!$this->acquired) {
66
            $this->acquired = $this->lockAdapter->acquireLock($this->resourceName);
67
        }
68
69
        return $this->acquired;
70
    }
71
72
    /**
73
     * @see \MetaborStd\Semaphore\MutexInterface::isLocked()
74
     */
75
    public function isLocked()
76
    {
77
        return $this->lockAdapter->isLocked($this->resourceName);
78
    }
79
80
    /**
81
     * release lock if mutex instance is destroyed.
82
     */
83
    public function __destruct()
84
    {
85
        $this->releaseLock();
86
    }
87
}
88