RedisMutex::acquire()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Mutex Library.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace AMF\Mutex\Adapter;
11
12
use AMF\Mutex\MutexInterface;
13
use Predis\ClientInterface;
14
15
/**
16
 * Mutex for redis.
17
 *
18
 * @author Amine Fattouch <[email protected]>
19
 */
20
class RedisMutex implements MutexInterface
21
{
22
    /**
23
     * @var Client
24
     */
25
    private $redis;
26
27
    /**
28
     * Constructor class.
29
     *
30
     * @param ClientInterface $redis Instance of redis client.
31
     */
32
    public function __construct(ClientInterface $redis)
33
    {
34
        $this->redis = $redis;
0 ignored issues
show
Documentation Bug introduced by
It seems like $redis of type object<Predis\ClientInterface> is incompatible with the declared type object<AMF\Mutex\Adapter\Client> of property $redis.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function acquire($key, $ttl)
41
    {
42
        $acquired = $this->redis->sadd($key, 1);
43
44
        if ($acquired === true) {
45
            $this->redis->expire($key, $ttl);
46
47
            return $key;
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function release($key)
57
    {
58
        if ($this->redis->exists($key)) {
59
            $this->redis->srem($key);
60
        }
61
    }
62
}
63