Gateway   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 37.25 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 19
loc 51
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A increase() 8 8 2
A set() 11 11 2
A getAll() 0 4 1
A get() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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