Completed
Push — pr32 ( 5d4eca...620873 )
by Kamil
02:47
created

MySQLPDOLock::acquireLock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
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
     * @param string $dsn
53
     * @param string $username
54
     * @param string $passwd
55
     * @param array $options
56
     * @param string $classname class name to create as PDO connection
57
     */
58
    public function __construct($dsn, $username = null, $passwd = null, $options = null, $classname = 'PDO')
59
    {
60
        parent::__construct();
61
62
        $this->dsn = $dsn;
63
        $this->username = $username;
64
        $this->passwd = $passwd;
65
        $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...
66
        $this->classname = $classname;
67
    }
68
69 5
    public function __clone()
70
    {
71 5
        parent::__clone();
72 5
        $this->pdo = array();
73 5
    }
74
75
    /**
76
     * Acquire lock
77
     *
78
     * @param  string   $name    name of lock
79
     * @param  null|int $timeout 1. null if you want blocking lock
80
     *                           2. 0 if you want just lock and go
81
     *                           3. $timeout > 0 if you want to wait for lock some time (in milliseconds)
82
     * @return bool
83
     */
84 16
    public function acquireLock($name, $timeout = null)
85
    {
86 16
        if (!$this->setupPDO($name)) {
87
            return false;
88
        }
89
90 16
        return parent::acquireLock($name, $timeout);
91
    }
92
93
    /**
94
     * @param  string $name
95
     * @param  bool   $blocking
96
     * @return bool
97
     */
98 16
    protected function getLock($name, $blocking)
99
    {
100 16
        return !$this->isLocked($name) && $this->pdo[$name]->query(
101 16
            sprintf(
102 16
                'SELECT GET_LOCK(%s, %d)',
103 16
                $this->pdo[$name]->quote($name),
104
                0
105
            ),
106 16
            PDO::FETCH_COLUMN,
107
            0
108 16
        )->fetch();
109
    }
110
111
    /**
112
     * Release lock
113
     *
114
     * @param  string $name name of lock
115
     * @return bool
116
     */
117 16
    public function releaseLock($name)
118
    {
119 16
        if (!$this->setupPDO($name)) {
120
            return false;
121
        }
122
123 16
        $released = (bool) $this->pdo[$name]->query(
124 16
            sprintf(
125 16
                'SELECT RELEASE_LOCK(%s)',
126 16
                $this->pdo[$name]->quote($name)
127
            ),
128 16
            PDO::FETCH_COLUMN,
129
            0
130 16
        )->fetch();
131
132 16
        if (!$released) {
133 2
            return false;
134
        }
135
136 16
        unset($this->pdo[$name]);
137 16
        unset($this->locks[$name]);
138
139 16
        return true;
140
    }
141
142
    /**
143
     * Check if lock is locked
144
     *
145
     * @param  string $name name of lock
146
     * @return bool
147
     */
148 16
    public function isLocked($name)
149
    {
150 16
        if (empty($this->pdo) && !$this->setupPDO($name)) {
151
            return false;
152
        }
153
154 16
        return !current($this->pdo)->query(
155 16
            sprintf(
156 16
                'SELECT IS_FREE_LOCK(%s)',
157 16
                current($this->pdo)->quote($name)
158
            ),
159 16
            PDO::FETCH_COLUMN,
160
            0
161 16
        )->fetch();
162
    }
163
164
    /**
165
     * @param  string $name
166
     * @return bool
167
     */
168 16
    protected function setupPDO($name)
169
    {
170 16
        if (isset($this->pdo[$name])) {
171 16
            return true;
172
        }
173
174 16
        $this->pdo[$name] = new $this->classname($this->dsn, $this->username, $this->passwd, $this->options);
175
176 16
        return true;
177
    }
178
179 5
    public function __destruct()
180
    {
181 5
        parent::__destruct();
182
183 5
        foreach($this->pdo as $name => $pdo) {
184 3
            unset($this->pdo[$name]);
185
        }
186 5
    }
187
}
188