ListLengthCheck   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 1
A run() 0 15 3
A getIdentifier() 0 3 1
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Database\Redis;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
/**
9
 * Class ListLength
10
 *
11
 * Check if the length of a redis list is not too big
12
 *
13
 * @package Leankoala\HealthFoundation\Check\Database\Redis
14
 */
15
class ListLengthCheck implements Check
16
{
17
    const IDENTFIER = 'base:database:redis:listLength';
18
19
    private $host;
20
    private $port;
21
    private $auth;
22
    private $listName;
23
24
    private $maxLength;
25
26
    public function init($listName, $maxLength, $auth = null, $host = 'localhost', $port = '6379')
27
    {
28
        $this->listName = $listName;
29
        $this->maxLength = $maxLength;
30
31
        $this->auth = $auth;
32
        $this->host = $host;
33
        $this->port = $port;
34
    }
35
36
    public function run()
37
    {
38
        $redis = new \Redis();
39
        $redis->connect($this->host);
40
41
        if ($this->auth) {
42
            $redis->auth($this->auth);
43
        }
44
45
        $length = $redis->lLen($this->listName);
46
47
        if ($length > $this->maxLength) {
48
            return new Result(Result::STATUS_FAIL, 'Too many elements in list ("' . $length . '"). Maximum was ' . $this->maxLength);
0 ignored issues
show
Bug introduced by
Are you sure $length of type integer|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            return new Result(Result::STATUS_FAIL, 'Too many elements in list ("' . /** @scrutinizer ignore-type */ $length . '"). Maximum was ' . $this->maxLength);
Loading history...
49
        } else {
50
            return new Result(Result::STATUS_FAIL, $length . ' elements in list. Maximum was ' . $this->maxLength);
51
        }
52
    }
53
54
    public function getIdentifier()
55
    {
56
        return self::IDENTFIER;
57
    }
58
}
59