Completed
Push — master ( 20d667...bbf4c7 )
by Lukas Kahwe
9s
created

Expression::check()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 3
nop 0
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
6
use ZendDiagnostics\Check\CheckInterface;
7
use ZendDiagnostics\Result\Failure;
8
use ZendDiagnostics\Result\Success;
9
use ZendDiagnostics\Result\Warning;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
class Expression implements CheckInterface
15
{
16
    private $label;
17
    private $warningCheck;
18
    private $criticalCheck;
19
    private $warningMessage;
20
    private $criticalMessage;
21
22
    public function __construct($label, $warningCheck = null, $criticalCheck = null, $warningMessage = null, $criticalMessage = null)
23
    {
24
        if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
25
            throw new \Exception('The symfony/expression-language is required for this check.');
26
        }
27
28
        if (!$warningCheck && !$criticalCheck) {
29
            throw new \InvalidArgumentException('Not checks set.');
30
        }
31
32
        $this->label = $label;
33
        $this->warningCheck = $warningCheck;
34
        $this->warningMessage = $warningMessage;
35
        $this->criticalCheck = $criticalCheck;
36
        $this->criticalMessage = $criticalMessage;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function check()
43
    {
44
        $language = $this->getExpressionLanguage();
45
46
        if ($this->criticalCheck && false === $language->evaluate($this->criticalCheck)) {
47
            return new Failure($this->criticalMessage);
48
        }
49
50
        if ($this->warningCheck && false === $language->evaluate($this->warningCheck)) {
51
            return new Warning($this->warningMessage);
52
        }
53
54
        return new Success();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getLabel()
61
    {
62
        return $this->label;
63
    }
64
65
    protected function getExpressionLanguage()
66
    {
67
        $language = new ExpressionLanguage();
68
        $language->register(
69
            'ini',
70
            function ($value) {
71
                return $value;
72
            },
73
            function ($arguments, $value) {
74
                return ini_get($value);
75
            }
76
        );
77
78
        return $language;
79
    }
80
}
81