Completed
Pull Request — master (#40)
by
unknown
09:28
created

MySQLPDOLock::releaseLock()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0032
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
use PDO;
13
14
/**
15
 * Lock implementor using MySQL PDO driver
16
 *
17
 * @author Kamil Dziedzic <[email protected]>
18
 */
19
class MySQLPDOLock extends LockAbstract
20
{
21
    /**
22
     * MySQL connections
23
     *
24
     * @var PDO[]
25
     */
26
    protected $pdo = array();
27
28
    /**
29
     * @var string
30
     */
31
    protected $dsn;
32
    /**
33
     * @var string
34
     */
35
    protected $username;
36
    /**
37
     * @var string
38
     */
39
    protected $passwd;
40
    /**
41
     * @var array
42
     */
43
    protected $options;
44
    /**
45
     * @var string
46
     */
47
    protected $classname;
48
49
    /**
50
     * Provide data for PDO connection
51
     *
52
     * @link http://php.net/manual/en/pdo.construct.php
53
     * @param string $dsn
54
     * @param string $username [optional]
55
     * @param string $passwd   [optional]
56
     * @param array  $options  [optional]
57
     *
58
     * @param string $classname class name to create as PDO connection,
59
     *                          by default this is PDO, but in tests we can inject MockPDO
60
     */
61
    public function __construct($dsn, $username = null, $passwd = null, $options = null, $classname = 'PDO')
62
    {
63
        parent::__construct();
64
65
        $this->dsn = $dsn;
66
        $this->username = $username;
67
        $this->passwd = $passwd;
68
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options can be null. However, the property $options is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
69
        $this->classname = $classname;
70
    }
71
72 10
    public function __clone()
73
    {
74 10
        parent::__clone();
75 10
        $this->pdo = array();
76 10
    }
77
78
    /**
79
     * Acquire lock
80
     *
81
     * @param  string   $name    name of lock
82
     * @param  null|int $timeout 1. null if you want blocking lock
83
     *                           2. 0 if you want just lock and go
84
     *                           3. $timeout > 0 if you want to wait for lock some time (in milliseconds)
85
     * @return bool
86
     */
87 17
    public function acquireLock($name, $timeout = null)
88
    {
89 17
        if (!$this->setupPDO($name)) {
90
            return false;
91
        }
92
93 17
        return parent::acquireLock($name, $timeout);
94
    }
95
96
    /**
97
     * @param  string $name
98
     * @param  bool   $blocking
99
     * @return bool
100
     */
101 17
    protected function getLock($name, $blocking)
102
    {
103 17
        return !$this->isLocked($name) && $this->pdo[$name]->query(
104 17
            sprintf(
105 17
                'SELECT GET_LOCK(%s, %d)',
106 17
                $this->pdo[$name]->quote($name),
107
                0
108
            ),
109 17
            PDO::FETCH_COLUMN,
110
            0
111 17
        )->fetch();
112
    }
113
114
    /**
115
     * Release lock
116
     *
117
     * @param  string $name name of lock
118
     * @return bool
119
     */
120 16
    public function releaseLock($name)
121
    {
122 16
        if (!$this->setupPDO($name)) {
123
            return false;
124
        }
125
126 16
        $released = (bool) $this->pdo[$name]->query(
127 16
            sprintf(
128 16
                'SELECT RELEASE_LOCK(%s)',
129 16
                $this->pdo[$name]->quote($name)
130
            ),
131 16
            PDO::FETCH_COLUMN,
132
            0
133 16
        )->fetch();
134
135 16
        if (!$released) {
136 4
            return false;
137
        }
138
139 16
        unset($this->pdo[$name]);
140 16
        unset($this->locks[$name]);
141
142 16
        return true;
143
    }
144
145
    /**
146
     * Check if lock is locked
147
     *
148
     * @param  string $name name of lock
149
     * @return bool
150
     */
151 17
    public function isLocked($name)
152
    {
153 17
        if (empty($this->pdo) && !$this->setupPDO($name)) {
154
            return false;
155
        }
156
157 17
        return !current($this->pdo)->query(
158 17
            sprintf(
159 17
                'SELECT IS_FREE_LOCK(%s)',
160 17
                current($this->pdo)->quote($name)
161
            ),
162 17
            PDO::FETCH_COLUMN,
163
            0
164 17
        )->fetch();
165
    }
166
167
    /**
168
     * @param  string $name
169
     * @return bool
170
     */
171 17
    protected function setupPDO($name)
172
    {
173 17
        if (isset($this->pdo[$name])) {
174 16
            return true;
175
        }
176
177 17
        $this->pdo[$name] = new $this->classname($this->dsn, $this->username, $this->passwd, $this->options);
178
179 17
        return true;
180
    }
181
182 10
    public function __destruct()
183
    {
184 10
        parent::__destruct();
185
186 10
        foreach($this->pdo as $name => $pdo) {
187 6
            unset($this->pdo[$name]);
188
        }
189 10
    }
190
}
191