Lock   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A timeout() 0 5 1
A acquire() 0 10 1
A release() 0 16 2
1
<?php
2
namespace PHPDaemon\Clients\Redis;
3
4
use PHPDaemon\Utils\Crypt;
5
6
/**
7
 * @package    NetworkClients
8
 * @subpackage RedisClient
9
 * @author     Vasily Zorin <[email protected]>
10
 */
11
class Lock
12
{
13
    use \PHPDaemon\Traits\ClassWatchdog;
14
    use \PHPDaemon\Traits\StaticObjectWatchdog;
15
16
    protected $pool;
17
    protected $timeout;
18
    protected $token;
19
    protected $key;
20
21
    /**
22
     * Constructor
23
     * @param string $key
24
     * @param integer $timeout
25
     * @param Pool $pool
26
     */
27
    public function __construct($key, $timeout, $pool)
28
    {
29
        $this->pool = $pool;
30
        $this->timeout = $timeout;
31
        $this->key = $key;
32
    }
33
34
    /**
35
     * @TODO
36
     * @param  integer $timeout
37
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be Lock?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39
    public function timeout($timeout)
40
    {
41
        $this->timeout = $timeout;
42
        return $this;
43
    }
44
45
    /**
46
     * @TODO
47
     * @param  callable $cb
48
     * @callback $cb ( )
49
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be Lock?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
50
     */
51
    public function acquire($cb)
52
    {
53
        Crypt::randomString(16, null, function ($token) use ($cb) {
54
            $this->token = $token;
55
            $this->pool->set($this->key, $this->token, 'NX', 'EX', $this->timeout, function ($redis) use ($cb) {
0 ignored issues
show
Documentation Bug introduced by
The method set does not exist on object<PHPDaemon\Clients\Redis\Pool>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
                $cb($this, $redis->result === 'OK', $redis);
57
            });
58
        });
59
        return $this;
60
    }
61
62
    /**
63
     * @TODO
64
     * @param  callable $cb
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @callback $cb ( )
66
     * @return this
0 ignored issues
show
Documentation introduced by
Should the return type not be Lock?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
67
     */
68
    public function release($cb = null)
69
    {
70
        $this->pool->eval(
0 ignored issues
show
Documentation Bug introduced by
The method eval does not exist on object<PHPDaemon\Clients\Redis\Pool>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
71
            'if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end',
72
            1,
73
            $this->key,
74
            $this->token,
75
            function ($redis) use ($cb) {
76
                if ($cb !== null) {
77
                    $cb($this, $redis->result, $redis);
78
                }
79
            }
80
        );
81
82
        return $this;
83
    }
84
}
85