Completed
Push — add_lock_to_statemachine ( 571155 )
by Oliver
02:16
created

LockAdapterMutex::acquireLock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 6
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