Gateway::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace BrainExe\Core\Stats;
4
5
use BrainExe\Core\Annotations\Service;
6
use BrainExe\Core\Traits\RedisTrait;
7
8
/**
9
 * @Service
10
 */
11
class Gateway
12
{
13
    const KEY = 'statistics';
14
15
    use RedisTrait;
16
17
    /**
18
     * @param int[] $values
19
     */
20 1 View Code Duplication
    public function increase(array $values)
21
    {
22 1
        $pipeline = $this->getRedis()->pipeline(['fire-and-forget' => true]);
23 1
        foreach ($values as $key => $value) {
24 1
            $pipeline->zincrby(self::KEY, $value, $key);
25
        }
26 1
        $pipeline->execute();
27 1
    }
28
29
    /**
30
     * @param string $key
31
     * @param int $value
32
     */
33 2 View Code Duplication
    public function set($key, $value)
34
    {
35 2
        $pipeline = $this->getRedis()->pipeline(['fire-and-forget' => true]);
36 2
        if ($value) {
37 1
            $pipeline->hset(self::KEY, $key, $value);
38
        } else {
39 1
            $pipeline->zrem(self::KEY, $key);
40
        }
41
42 2
        $pipeline->execute();
43 2
    }
44
45
    /**
46
     * @return int[]
47
     */
48 1
    public function getAll() : array
49
    {
50 1
        return $this->getRedis()->zrevrangebyscore(self::KEY, '+inf', 0, ['withscores' => true]);
51
    }
52
53
    /**
54
     * @param string $key
55
     * @return int
56
     */
57 1
    public function get($key)
58
    {
59 1
        return (int)$this->getRedis()->hget(self::KEY, $key);
60
    }
61
}
62