Completed
Push — 2.0 ( 0d93f2...14c778 )
by Marco
16:42
created

AbstractLocker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 35
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
lock() 0 1 ?
release() 0 1 ?
A writeLock() 0 9 2
A readLock() 0 9 2
A releaseLock() 0 7 2
1
<?php namespace Comodojo\Extender\Components;
2
3
use \Exception;
4
5
/**
6
 * Lock file manager (static methods)
7
 *
8
 * @package     Comodojo extender
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     GPL-3.0+
11
 *
12
 * LICENSE:
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 */
27
28
abstract class AbstractLocker {
29
30
    abstract public function lock();
31
32
    abstract public function release();
33
34
    protected static function writeLock($file, $data) {
35
36
        $lock = file_put_contents($file, $data);
37
38
        if ( $lock === false ) throw new Exception("Cannot write lock file");
39
40
        return $lock;
41
42
    }
43
44
    protected static function readLock($file) {
45
46
        $data = file_get_contents($file);
47
48
        if ( $data === false ) throw new Exception("Cannot read lock file");
49
50
        return $data;
51
52
    }
53
54
    protected static function releaseLock($file) {
55
56
        $lock = file_exists($file) ? unlink($file) : true;
57
58
        return $lock;
59
60
    }
61
62
}
63