PhpRedisLock::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of ninja-mutex.
4
 *
5
 * (C) leo108 <[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 Redis;
13
14
/**
15
 * Lock implementor using PHPRedis
16
 *
17
 * @author leo108 <[email protected]>
18
 */
19
class PhpRedisLock extends LockAbstract
20
{
21
    /**
22
     * Redis connection
23
     *
24
     * @var
25
     */
26
    protected $client;
27
28
    /**
29
     * @param $client Redis
30
     */
31
    public function __construct($client)
32
    {
33
        parent::__construct();
34
35
        $this->client = $client;
36
    }
37
38
    /**
39
     * @param  string $name
40
     * @param  bool   $blocking
41
     * @return bool
42
     */
43 33
    protected function getLock($name, $blocking)
44
    {
45 33
        if (!$this->client->setnx($name, serialize($this->getLockInformation()))) {
46 4
            return false;
47
        }
48
49 33
        return true;
50
    }
51
52
    /**
53
     * Release lock
54
     *
55
     * @param  string $name name of lock
56
     * @return bool
57
     */
58 33
    public function releaseLock($name)
59
    {
60 33
        if (isset($this->locks[$name]) && $this->client->del($name)) {
61 33
            unset($this->locks[$name]);
62
63 33
            return true;
64
        }
65
66 5
        return false;
67
    }
68
69
    /**
70
     * Check if lock is locked
71
     *
72
     * @param  string $name name of lock
73
     * @return bool
74
     */
75 15
    public function isLocked($name)
76
    {
77 15
        return false !== $this->client->get($name);
78
    }
79
}
80