Mutex   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 108
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A acquireLock() 0 11 3
A releaseLock() 0 13 4
A __destruct() 0 12 3
A isAcquired() 0 4 1
A isLocked() 0 4 1
1
<?php
2
/**
3
 * This file is part of ninja-mutex.
4
 *
5
 * (C) Kamil Dziedzic <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NinjaMutex;
11
12
use NinjaMutex\Lock\LockInterface;
13
14
/**
15
 * Mutex
16
 *
17
 * @author Kamil Dziedzic <[email protected]>
18
 */
19
class Mutex
20
{
21
    /**
22
     * Lock implementor
23
     *
24
     * @var LockInterface
25
     */
26
    protected $lockImplementor;
27
28
    /**
29
     * Name of lock
30
     *
31
     * @var string
32
     */
33
    protected $name;
34
35
    /**
36
     * Lock counter to protect against recursive deadlock
37
     *
38
     * @var integer
39
     */
40
    protected $counter = 0;
41
42
    /**
43
     * @param string        $name
44
     * @param LockInterface $lockImplementor
45
     */
46 155
    public function __construct($name, LockInterface $lockImplementor)
47
    {
48 155
        $this->name = $name;
49 155
        $this->lockImplementor = $lockImplementor;
50 155
    }
51
52
    /**
53
     * @param  int|null $timeout
54
     * @return bool
55
     */
56 127
    public function acquireLock($timeout = null)
57
    {
58 127
        if ($this->counter > 0 ||
59 127
            $this->lockImplementor->acquireLock($this->name, $timeout)) {
60 127
            $this->counter++;
61
62 127
            return true;
63
        }
64
65 28
        return false;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 127
    public function releaseLock()
72
    {
73 127
        if ($this->counter > 0) {
74 127
            $this->counter--;
75 127
            if ($this->counter > 0 ||
76 127
                $this->lockImplementor->releaseLock($this->name)) {
77 127
                return true;
78
            }
79 1
            $this->counter++;
80 1
        }
81
82 15
        return false;
83
    }
84
85
    /**
86
     * Try to release any obtained locks when object is destroyed
87
     *
88
     * This is a safe guard for cases when your php script dies unexpectedly.
89
     * It's not guaranteed it will work either.
90
     *
91
     * You should not depend on __destruct() to release your locks,
92
     * instead release them with `$released = $this->releaseLock()`A
93
     * and check `$released` if lock was properly released
94
     * @throws UnrecoverableMutexException
95 155
     */
96
    public function __destruct()
97 155
    {
98 85
        while ($this->isAcquired()) {
99 85
            $released = $this->releaseLock();
100 1
            if (!$released) {
101 1
                throw new UnrecoverableMutexException(sprintf(
102 1
                    'Cannot release lock in Mutex __destruct(): %s',
103 1
                    $this->name
104
                ));
105 85
            }
106 155
        }
107
    }
108
109
    /**
110
     * Check if Mutex is acquired
111
     *
112
     * @return bool
113 155
     */
114
    public function isAcquired()
115 155
    {
116
        return $this->counter > 0;
117
    }
118
119
    /**
120
     * @return bool
121 84
     */
122
    public function isLocked()
123 84
    {
124
        return $this->lockImplementor->isLocked($this->name);
125
    }
126
}
127