Logger::setUid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @property integer $uid
4
 * @property string $key
5
 * @property integer $level
6
 * @property array $counters
7
 */
8
class Logger extends CModel
9
{
10
    private $key = '';
11
    private $level;
12
    private $uid;
13
14
    public function attributeNames()
15
    {
16
        return [];
17
    }
18
19
    public function getCounters($postfix = ':all')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21
        if (!$this->uid) {
22
            return false;
23
        }
24
        
25
        $redis = Yii::app()->redis->getClient();
26
        $key = $this->getCounterKey();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
27
28
        return $redis->hGetAll($key . $postfix);
29
    }
30
31
    public function setKey($key)
32
    {
33
        $this->key = $key;
34
    }
35
36
    public function setLevel($level)
37
    {
38
        $this->level = (int)$level;
39
    }
40
41
    public function setUid($uid)
42
    {
43
        $this->uid = (int)$uid;
44
    }
45
46
    /**
47
     * @param string $value
48
     */
49
    public function addToSet($value)
50
    {
51
        Yii::app()->redis->getClient()->rPush('debug:'.$this->key, $value);
52
    }
53
54
    /**
55
     * @param string $field
56
     * @param integer $value
57
     */
58
    public function increment($field, $value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        if (!$this->uid) {
61
            return false;
62
        }
63
64
        $redis = Yii::app()->redis->getClient();
65
        $key = $this->getCounterKey();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
66
67
        //aggregated
68
        $return = $redis->hIncrBy($key.':all', $field, (int)$value);
69
        if ($this->level) {
70
            //by uid+level
71
            $redis->hIncrBy($key.':levels:'.$this->level, $field, (int)$value);
72
            //by level
73
            $redis->hIncrBy('counter:levels:'.$this->level, $field, (int)$value);
74
        }
75
76
77
        return $return;
78
    }
79
80
    private function getCounterKey()
81
    {
82
        $suid = (string)$this->uid;
83
        return 'counter:' . $suid[0] . ':' . $suid[1] . ':' .$suid[2] . ':' . $suid;
84
    }
85
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
86