Completed
Push — fix_travis ( 785058...ce7855 )
by Kamil
02:06
created

Mutex   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 13
c 2
b 2
f 0
lcom 1
cbo 2
dl 0
loc 107
ccs 0
cts 46
cp 0
rs 10

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
    public function __construct($name, LockInterface $lockImplementor)
47
    {
48
        $this->name = $name;
49
        $this->lockImplementor = $lockImplementor;
50
    }
51
52
    /**
53
     * @param  int|null $timeout
54
     * @return bool
55
     */
56
    public function acquireLock($timeout = null)
57
    {
58
        if ($this->counter > 0 ||
59
            $this->lockImplementor->acquireLock($this->name, $timeout)) {
60
            $this->counter++;
61
62
            return true;
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function releaseLock()
72
    {
73
        if ($this->counter > 0) {
74
            $this->counter--;
75
            if ($this->counter > 0 ||
76
                $this->lockImplementor->releaseLock($this->name)) {
77
                return true;
78
            }
79
            $this->counter++;
80
        }
81
82
        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
     */
95
    public function __destruct()
96
    {
97
        while ($this->isAcquired()) {
98
            $released = $this->releaseLock();
99
            if (!$released) {
100
                throw new UnrecoverableMutexException(sprintf(
101
                    'Cannot release lock in Mutex __destruct(): %s',
102
                    $this->name
103
                ));
104
            }
105
        }
106
    }
107
108
    /**
109
     * Check if Mutex is acquired
110
     *
111
     * @return bool
112
     */
113
    public function isAcquired()
114
    {
115
        return $this->counter > 0;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    public function isLocked()
122
    {
123
        return $this->lockImplementor->isLocked($this->name);
124
    }
125
}
126