FileLock::__destory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
ccs 0
cts 5
cp 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/8/21
6
 * Time: 14:30
7
 */
8
9
namespace Jenner\SimpleFork\Lock;
10
11
12
/**
13
 * file lock
14
 *
15
 * @package Jenner\SimpleFork\Lock
16
 */
17
class FileLock implements LockInterface
18
{
19
    /**
20
     * @var string lock file
21
     */
22
    protected $file;
23
24
    /**
25
     * @var resource
26
     */
27
    protected $fp;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $locked = false;
33
34
    /**
35
     * @param $file
36
     */
37 12
    private function __construct($file)
38
    {
39 12
        if (!file_exists($file) || !is_readable($file)) {
40
            throw new \RuntimeException("{$file} is not exists or not readable");
41
        }
42 12
        $this->fp = fopen($file, "r+");
43 12
        if (!is_resource($this->fp)) {
44
            throw new \RuntimeException("open {$file} failed");
45
        }
46 12
    }
47
48
    /**
49
     * create a file lock instance
50
     * if the file is not exists, it will be created
51
     *
52
     * @param string $file lock file
53
     * @return FileLock
54
     */
55 12
    public static function create($file)
56
    {
57 12
        return new FileLock($file);
58
    }
59
60
    /**
61
     * get a lock
62
     *
63
     * @param bool $blocking
64
     * @return mixed
65
     */
66 9
    public function acquire($blocking = true)
67
    {
68 9
        if ($this->locked) {
69 3
            throw new \RuntimeException('already lock by yourself');
70
        }
71
72 9
        if ($blocking) {
73 6
            $locked = flock($this->fp, LOCK_EX);
74 6
        } else {
75 3
            $locked = flock($this->fp, LOCK_EX | LOCK_NB);
76
        }
77
78 9
        if ($locked !== true) {
79 3
            return false;
80
        }
81 9
        $this->locked = true;
82
83 9
        return true;
84
    }
85
86
    /**
87
     * is locked
88
     *
89
     * @return mixed
90
     */
91
    public function isLocked()
92
    {
93
        return $this->locked === true ? true : false;
94
    }
95
96
    /**
97
     *
98
     */
99
    public function __destory()
100
    {
101
        if ($this->locked) {
102
            $this->release();
103
        }
104
    }
105
106
    /**
107
     * release lock
108
     *
109
     * @return mixed
110
     */
111 9
    public function release()
112
    {
113 9
        if (!$this->locked) {
114 3
            throw new \RuntimeException('release a non lock');
115
        }
116
117 6
        $unlock = flock($this->fp, LOCK_UN);
118 6
        fclose($this->fp);
119 6
        if ($unlock !== true) {
120
            return false;
121
        }
122 6
        $this->locked = false;
123
124 6
        return true;
125
    }
126
}