Completed
Push — master ( 85f6f1...997e4e )
by Hu
02:27
created

Semaphore::_stringToSemKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
ccs 7
cts 7
cp 1
cc 2
eloc 6
nc 2
nop 1
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 9
    private function __construct($key, $count = 1)
36
    {
37 9
        if (($this->lock_id = sem_get($this->_stringToSemKey($key), $count)) === false) {
38
            throw new \RuntimeException("Cannot create semaphore for key: {$key}");
39
        }
40 9
    }
41
42
    /**
43
     * Semaphore requires a numeric value as the key
44
     *
45
     * @param $identifier
46
     * @return int
47
     */
48 9
    protected function _stringToSemKey($identifier)
49
    {
50 9
        $md5 = md5($identifier);
51 9
        $key = 0;
52 9
        for ($i = 0; $i < 32; $i++) {
53 9
            $key += ord($md5{$i}) * $i;
54 9
        }
55 9
        return $key;
56
    }
57
58
    /**
59
     * create a lock instance
60
     *
61
     * @param $key
62
     * @return Semaphore
63
     */
64 9
    public static function create($key)
65
    {
66 9
        return new Semaphore($key);
67
    }
68
69
    /**
70
     * release lock
71
     *
72
     * @throws \RuntimeException
73
     */
74 9
    public function __destruct()
75
    {
76 9
        if ($this->isLocked()) {
77 3
            $this->release();
78 3
        }
79 9
    }
80
81
    /**
82
     * is locked
83
     *
84
     * @return bool
85
     */
86 9
    public function isLocked()
87
    {
88 9
        return $this->locked === true ? true : false;
89
    }
90
91
    /**
92
     * release lock
93
     *
94
     * @return bool
95
     * @throws \RuntimeException
96
     */
97 9
    public function release()
98
    {
99 9
        if (!$this->locked) {
100 3
            throw new \RuntimeException("release a non lock");
101
        }
102
103 6
        if (!sem_release($this->lock_id)) {
104
            return false;
105
        }
106 6
        $this->locked = false;
107
108 6
        return true;
109
    }
110
111
    /**
112
     * get a lock
113
     *
114
     * @param bool $blocking
115
     * @return bool
116
     */
117 6
    public function acquire($blocking = true)
118
    {
119 6
        if ($this->locked) {
120 3
            throw new \RuntimeException('already lock by yourself');
121
        }
122
123 6
        if ($blocking === false) {
124
            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
            if (!sem_acquire($this->lock_id, true)) {
128
                return false;
129
            }
130
            $this->locked = true;
131
132
            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
}