Gateway::getAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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