Completed
Push — master ( 102d2e...8c0f5d )
by Kamil
03:50
created

DirectoryLock::getDirectoryPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of ninja-mutex.
4
 *
5
 * (C) Kamil Dziedzic <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NinjaMutex\Lock;
11
12
/**
13
 * Lock implementor using mkdir
14
 *
15
 * @author Jan Voracek <[email protected]>
16
 */
17
class DirectoryLock extends LockAbstract
18
{
19
    protected $dirname;
20
21
    /**
22
     * @param string $dirname
23
     */
24
    public function __construct($dirname)
25
    {
26
        parent::__construct();
27
28
        $this->dirname = $dirname;
29
    }
30
31
    /**
32
     * @param  string $name
33
     * @param  bool   $blocking
34
     * @return bool
35
     */
36 32
    protected function getLock($name, $blocking)
37
    {
38 32
        while (!@mkdir($this->getDirectoryPath($name))) {
39 4
            if (!$blocking) {
40 4
                return false;
41
            }
42
43
            usleep(rand(5000, 20000));
44
        }
45
46 32
        return true;
47
    }
48
49
    /**
50
     * Release lock
51
     *
52
     * @param  string $name name of lock
53
     * @return bool
54
     */
55 32
    public function releaseLock($name)
56
    {
57 32
        if (isset($this->locks[$name])) {
58 32
            rmdir($this->getDirectoryPath($name));
59 32
            unset($this->locks[$name]);
60
61 32
            return true;
62
        }
63
64 4
        return false;
65
    }
66
67
    /**
68
     * @param  string $name
69
     * @return string
70
     */
71 32
    protected function getDirectoryPath($name)
72
    {
73 32
        return $this->dirname . DIRECTORY_SEPARATOR . $name . '.lock';
74
    }
75
76
    /**
77
     * Check if lock is locked
78
     *
79
     * @param  string $name name of lock
80
     * @return bool
81
     */
82 14
    public function isLocked($name)
83
    {
84 14
        if ($this->acquireLock($name, false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a null|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85 6
            return !$this->releaseLock($name);
86
        }
87
88 14
        return true;
89
    }
90
}
91