Completed
Push — master ( 1fa08e...85f6f1 )
by Hu
02:27
created

Semaphore::isLocked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/8/12
6
 * Time: 20:52
7
 */
8
9
namespace Jenner\SimpleFork\Lock;
10
11
12
/**
13
 * sem lock
14
 *
15
 * @package Jenner\SimpleFork\Lock
16
 */
17
class Semaphore implements LockInterface
18
{
19
    /**
20
     * @var
21
     */
22
    private $lock_id;
23
    /**
24
     * @var bool
25
     */
26
    private $locked = false;
27
28
    /**
29
     * init a lock
30
     *
31
     * @param $key
32
     * @param $count
33
     * @throws \RuntimeException
34
     */
35 10
    private function __construct($key, $count = 1)
36
    {
37 10
        if (($this->lock_id = sem_get($this->_stringToSemKey($key), $count)) === false) {
38
            throw new \RuntimeException("Cannot create semaphore for key: {$key}");
39
        }
40 10
    }
41
42
    /**
43
     * Semaphore requires a numeric value as the key
44
     *
45
     * @param $identifier
46
     * @return int
47
     */
48 10
    protected function _stringToSemKey($identifier)
49
    {
50 10
        $md5 = md5($identifier);
51 10
        $key = 0;
52 10
        for ($i = 0; $i < 32; $i++) {
53 10
            $key += ord($md5{$i}) * $i;
54 10
        }
55 10
        return $key;
56
    }
57
58
    /**
59
     * create a lock instance
60
     *
61
     * @param $key
62
     * @return Semaphore
63
     */
64 10
    public static function create($key)
65
    {
66 10
        return new Semaphore($key);
67
    }
68
69
    /**
70
     * release lock
71
     *
72
     * @throws \RuntimeException
73
     */
74 10
    public function __destruct()
75
    {
76 10
        if ($this->isLocked()) {
77 3
            $this->release();
78 3
        }
79 10
    }
80
81
    /**
82
     * is locked
83
     *
84
     * @return bool
85
     */
86 10
    public function isLocked()
87
    {
88 10
        return $this->locked === true ? true : false;
89
    }
90
91
    /**
92
     * release lock
93
     *
94
     * @return bool
95
     * @throws \RuntimeException
96
     */
97 10
    public function release()
98
    {
99 10
        if (!$this->locked) {
100 3
            throw new \RuntimeException("release a non lock");
101
        }
102
103 7
        if (!sem_release($this->lock_id)) {
104
            return false;
105
        }
106 7
        $this->locked = false;
107
108 7
        return true;
109
    }
110
111
    /**
112
     * get a lock
113
     *
114
     * @param bool $blocking
115
     * @return bool
116
     */
117 7
    public function acquire($blocking = true)
118
    {
119 7
        if ($this->locked) {
120 3
            throw new \RuntimeException('already lock by yourself');
121
        }
122
123 7
        if ($blocking === false) {
124 1
            if (version_compare(PHP_VERSION, '5.6.0') < 0) {
125
                throw new \RuntimeException('php version is at least 5.6.0 for param blocking');
126
            }
127 1
            if (!sem_acquire($this->lock_id, true)) {
128 1
                return false;
129
            }
130 1
            $this->locked = true;
131
132 1
            return true;
133
        }
134
135 6
        if (!sem_acquire($this->lock_id)) {
136
            return false;
137
        }
138 6
        $this->locked = true;
139
140 6
        return true;
141
    }
142
}