Expression   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 61
ccs 25
cts 29
cp 0.8621
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A check() 0 14 5
A getLabel() 0 4 1
A getExpressionLanguage() 0 15 1
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use Laminas\Diagnostics\Check\CheckInterface;
6
use Laminas\Diagnostics\Result\Failure;
7
use Laminas\Diagnostics\Result\Success;
8
use Laminas\Diagnostics\Result\Warning;
9
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
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 5
    public function __construct($label, $warningCheck = null, $criticalCheck = null, $warningMessage = null, $criticalMessage = null)
23
    {
24 5
        if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
25
            throw new \Exception('The symfony/expression-language is required for this check.');
26
        }
27
28 5
        if (!$warningCheck && !$criticalCheck) {
29
            throw new \InvalidArgumentException('Not checks set.');
30
        }
31
32 5
        $this->label = $label;
33 5
        $this->warningCheck = $warningCheck;
34 5
        $this->warningMessage = $warningMessage;
35 5
        $this->criticalCheck = $criticalCheck;
36 5
        $this->criticalMessage = $criticalMessage;
37 5
    }
38
39 4
    public function check()
40
    {
41 4
        $language = $this->getExpressionLanguage();
42
43 4
        if ($this->criticalCheck && false === $language->evaluate($this->criticalCheck)) {
44 2
            return new Failure($this->criticalMessage);
45
        }
46
47 2
        if ($this->warningCheck && false === $language->evaluate($this->warningCheck)) {
48 1
            return new Warning($this->warningMessage);
49
        }
50
51 1
        return new Success();
52
    }
53
54 4
    public function getLabel()
55
    {
56 4
        return $this->label;
57
    }
58
59 4
    protected function getExpressionLanguage()
60
    {
61 4
        $language = new ExpressionLanguage();
62 4
        $language->register(
63 4
            'ini',
64
            function ($value) {
65
                return $value;
66 4
            },
67
            function ($arguments, $value) {
68
                return ini_get($value);
69 4
            }
70
        );
71
72 4
        return $language;
73
    }
74
}
75