PredisRedisLock   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 61
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLock() 0 8 2
A releaseLock() 0 10 3
A isLocked() 0 4 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
use Predis;
13
14
/**
15
 * Lock implementor using Predis (client library for Redis)
16
 *
17
 * @author Kamil Dziedzic <[email protected]>
18
 */
19
class PredisRedisLock extends LockAbstract
20
{
21
    /**
22
     * Predis connection
23
     *
24
     * @var
25
     */
26
    protected $client;
27
28
    /**
29
     * @param $client Predis\Client
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 null !== $this->client->get($name);
78
    }
79
}
80