RedisLock   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 26
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A lock() 0 6 1
A unlock() 0 4 1
1
<?php
2
3
namespace BrainExe\Core\Application;
4
5
use BrainExe\Core\Annotations\Service;
6
use BrainExe\Core\Traits\RedisTrait;
7
8
/**
9
 * @Service
10
 * @api
11
 */
12
class RedisLock
13
{
14
    private const PREFIX = 'lock:';
15
16
    use RedisTrait;
17
18
    /**
19
     * @param string $name
20
     * @param int $lockTime
21
     * @return bool $got_lock
22
     */
23 1
    public function lock(string $name, int $lockTime) : bool
24
    {
25 1
        $redis = $this->getRedis();
26
27 1
        return (bool)$redis->set(self::PREFIX . $name, '1', 'EX', $lockTime, 'NX');
28
    }
29
30
    /**
31
     * @param string $name
32
     */
33 1
    public function unlock(string $name)
34
    {
35 1
        $this->getRedis()->del(self::PREFIX . $name);
36 1
    }
37
}
38