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

PhpRedisLock   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 1
dl 61
loc 61
ccs 0
cts 24
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A getLock() 8 8 2
A releaseLock() 10 10 3
A isLocked() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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