Completed
Pull Request — master (#9)
by
unknown
02:28
created

Semaphore::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
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
        $this->remove();
80 9
    }
81
82
    /**
83
     * is locked
84
     *
85
     * @return bool
86
     */
87 10
    public function isLocked()
88
    {
89 10
        return $this->locked === true ? true : false;
90
    }
91
92
    /**
93
     * release lock
94
     *
95
     * @return bool
96
     * @throws \RuntimeException
97
     */
98 9
    public function release()
99
    {
100 9
        if (!$this->locked) {
101 3
            throw new \RuntimeException("release a non lock");
102
        }
103
104 6
        if (!sem_release($this->lock_id)) {
105
            return false;
106
        }
107 6
        $this->locked = false;
108
109 6
        return true;
110
    }
111
112
    /**
113
     * get a lock
114
     *
115
     * @param bool $blocking
116
     * @return bool
117
     */
118 7
    public function acquire($blocking = true)
119
    {
120 7
        if ($this->locked) {
121 3
            throw new \RuntimeException('already lock by yourself');
122
        }
123
124 7
        if ($blocking === false) {
125 1
            if (version_compare(PHP_VERSION, '5.6.0') < 0) {
126
                throw new \RuntimeException('php version is at least 5.6.0 for param blocking');
127
            }
128 1
            if (!sem_acquire($this->lock_id, true)) {
129 1
                return false;
130
            }
131
            $this->locked = true;
132
133
            return true;
134
        }
135
136 6
        if (!sem_acquire($this->lock_id)) {
137
            return false;
138
        }
139 6
        $this->locked = true;
140
141 6
        return true;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 10
    public function remove()
148
    {
149 10
        if (!sem_remove($this->lock_id)) throw new \RuntimeException("Can't remove semaphore.");
150 9
        return true;
151
    }
152
}