Validator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Rule;
11
12
13
/**
14
 * Rule validator
15
 *
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class Validator {
19
20
    /**
21
     * Critical
22
     */
23
    const CRITICAL = 'critical';
24
25
    /**
26
     * Warning
27
     */
28
    const WARNING = 'warning';
29
30
    /**
31
     * Good
32
     */
33
    const GOOD = 'good';
34
35
    /**
36
     * unknown
37
     */
38
    const UNKNOWN = 'unknown';
39
40
    /**
41
     * @var RuleSet
42
     */
43
    private $ruleSet;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param RuleSet $ruleSet
49
     */
50
    function __construct(RuleSet $ruleSet)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52
        $this->ruleSet = $ruleSet;
53
    }
54
55
    /**
56
     * Validate score
57
     *
58
     * @param $key
59
     * @param $value
60
     * @return string
61
     */
62
    public function validate($key, $value) {
63
        $rule = $this->ruleSet->getRule($key);
64
65
        if(!is_array($rule) || !is_numeric($value)) {
66
            return self::UNKNOWN;
67
        }
68
69
        // according order
70
        if($rule[0] < $rule[2]) {
71
            // critical < warn < good
72
            switch(true) {
73
                case $value < $rule[1]:
74
                    return self::CRITICAL;
75
                case $value >= $rule[1] && $value < $rule[2]:
76
                    return self::WARNING;
77
                default:
78
                    return self::GOOD;
79
            }
80
        }
81
        // critical > warn > good
82
        switch(true) {
83
            case $value > $rule[1]:
84
                return self::CRITICAL;
85
            case $value < $rule[2]:
86
                return self::GOOD;
87
            default:
88
                return self::WARNING;
89
        }
90
    }
91
92
    /**
93
     * Get used ruleset
94
     *
95
     * @return RuleSet
96
     */
97
    public function getRuleSet() {
98
        return $this->ruleSet;
99
    }
100
}