GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RedisLockStore::getDrift()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace RemiSan\Lock\LockStore;
4
5
use RemiSan\Lock\Lock;
6
use RemiSan\Lock\LockStore;
7
8
class RedisLockStore implements LockStore
9
{
10
    /** @var float */
11
    const CLOCK_DRIFT_FACTOR = 0.01;
12
13
    /** @var int */
14
    const REDIS_EXPIRES_PRECISION = 2;
15
16
    /** @var \Redis */
17
    private $redis;
18
19
    /**
20
     * RedisLockStore constructor.
21
     *
22
     * @param \Redis $redis
23
     */
24 24
    public function __construct(\Redis $redis)
25
    {
26 24
        if (!$redis->isConnected()) {
27 3
            throw new \InvalidArgumentException('The Redis instance must be connected.');
28
        }
29 24
        $this->redis = $redis;
30 24
    }
31
    /**
32
     * {@inheritdoc}
33
     */
34 6
    public function set(Lock $lock, $ttl = null)
35
    {
36 6
        $options = ['NX'];
37
38 6
        if ($ttl !== null && $ttl > 0) {
39 3
            $options['PX'] = (int) $ttl;
40 2
        }
41
42 6
        return (bool) $this->redis->set($lock->getResource(), (string) $lock->getToken(), $options);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 6
    public function exists($resource)
49
    {
50 6
        return (bool) $this->redis->get($resource);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 6
    public function delete(Lock $lock)
57
    {
58
        $script = '
59
            if redis.call("GET", KEYS[1]) == ARGV[1] then
60
                return redis.call("DEL", KEYS[1])
61
            else
62
                return 0
63
            end
64 6
        ';
65
66 6
        return (bool) $this->redis->evaluate(
67 4
            $script,
68 6
            [$lock->getResource(), (string) $lock->getToken()],
69 2
            1
70 4
        );
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 3
    public function getDrift($ttl)
77
    {
78
        // Add 2 milliseconds to the drift to account for Redis expires
79
        // precision, which is 1 millisecond, plus 1 millisecond min drift
80
        // for small TTLs.
81
82 3
        $minDrift = ($ttl) ? (int) ceil($ttl * self::CLOCK_DRIFT_FACTOR) : 0;
83
84 3
        return $minDrift + self::REDIS_EXPIRES_PRECISION;
85
    }
86
}
87