Completed
Push — pr21 ( ec1d9e )
by Kamil
02:27
created

MemcachedLock   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 1
dl 104
loc 104
ccs 23
cts 25
cp 0.92
rs 10

6 Methods

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

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) 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 Memcached;
13
14
/**
15
 * Lock implementor using Memcached
16
 *
17
 * @author Kamil Dziedzic <[email protected]>
18
 */
19 View Code Duplication
class MemcachedLock extends LockAbstract implements LockExpirationInterface
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
     * Maximum expiration time in seconds (30 days)
23
     * http://php.net/manual/en/memcached.add.php
24
     */
25
    const MAX_EXPIRATION = 2592000;
26
27
    /**
28
     * Memcache connection
29
     *
30
     * @var Memcached
31
     */
32
    protected $memcached;
33
34
    /**
35
     * @var int Expiration time of the lock in seconds
36
     */
37
    protected $expiration = 0;
38
39
    /**
40
     * @param Memcached $memcached
41
     */
42 1
    public function __construct($memcached)
43
    {
44 1
        parent::__construct();
45
46 1
        $this->memcached = $memcached;
47 1
    }
48
49
    /**
50
     * @param int $expiration Expiration time of the lock in seconds. If it's equal to zero (default), the lock will never expire.
51
     *                        Max 2592000s (30 days), if greater it will be capped to 2592000 without throwing an error.
52
     *                        WARNING: Using value higher than 0 may lead to race conditions. If you set too low expiration time
53
     *                        e.g. 30s and critical section will run for 31s another process will gain lock at the same time,
54
     *                        leading to unpredicted behaviour. Use with caution.
55
     */
56 1
    public function setExpiration($expiration)
57
    {
58 1
        if ($expiration > static::MAX_EXPIRATION) {
59
            $expiration = static::MAX_EXPIRATION;
60
        }
61 1
        $this->expiration = $expiration;
62 1
    }
63
64
    /**
65
     * Clear lock without releasing it
66
     * Do not use this method unless you know what you do
67
     *
68
     * @param  string $name name of lock
69
     * @return bool
70
     */
71 1
    public function clearLock($name)
72
    {
73 1
        if (!isset($this->locks[$name])) {
74
            return false;
75
        }
76
77 1
        unset($this->locks[$name]);
78 1
        return true;
79
    }
80
81
    /**
82
     * @param  string $name name of lock
83
     * @param  bool   $blocking
84
     * @return bool
85
     */
86 34
    protected function getLock($name, $blocking)
87
    {
88 34
        if (!$this->memcached->add($name, serialize($this->getLockInformation()), $this->expiration)) {
89 5
            return false;
90
        }
91
92 34
        return true;
93
    }
94
95
    /**
96
     * Release lock
97
     *
98
     * @param  string $name name of lock
99
     * @return bool
100
     */
101 34
    public function releaseLock($name)
102
    {
103 34
        if (isset($this->locks[$name]) && $this->memcached->delete($name)) {
104 34
            unset($this->locks[$name]);
105
106 34
            return true;
107
        }
108
109 5
        return false;
110
    }
111
112
    /**
113
     * Check if lock is locked
114
     *
115
     * @param  string $name name of lock
116
     * @return bool
117
     */
118 15
    public function isLocked($name)
119
    {
120 15
        return false !== $this->memcached->get($name);
121
    }
122
}
123
124