Completed
Push — fix_travis ( 785058...ce7855 )
by Kamil
02:06
created

PhpRedisLock::isLocked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
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 View Code Duplication
class PhpRedisLock extends LockAbstract
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * phpredis connection
23
     *
24
     * @var
25
     */
26
    protected $client;
27
28
    /**
29
     * @param $client Redis
30
     */
31
    public function __construct(Redis $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
    protected function getLock($name, $blocking)
44
    {
45
        if (!$this->client->setnx($name, serialize($this->getLockInformation()))) {
46
            return false;
47
        }
48
49
        return true;
50
    }
51
52
    /**
53
     * Release lock
54
     *
55
     * @param  string $name name of lock
56
     * @return bool
57
     */
58
    public function releaseLock($name)
59
    {
60
        if (isset($this->locks[$name]) && $this->client->del($name)) {
61
            unset($this->locks[$name]);
62
63
            return true;
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * Check if lock is locked
71
     *
72
     * @param  string $name name of lock
73
     * @return bool
74
     */
75
    public function isLocked($name)
76
    {
77
        return false !== $this->client->get($name);
78
    }
79
}
80