Completed
Push — master ( 872461...e70e08 )
by Vladimir
06:10
created

Check::check()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Check\php\Expression;
13
14
use Symfony\Component\ExpressionLanguage\Expression;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use Tvi\MonitorBundle\Exception\FeatureRequired;
17
use ZendDiagnostics\Result\Success;
18
use ZendDiagnostics\Result\Warning;
19
use ZendDiagnostics\Result\Failure;
20
21
use Tvi\MonitorBundle\Check\CheckInterface;
22
use Tvi\MonitorBundle\Check\CheckTrait;
23
24
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
 * @author Vladimir Turnaev <[email protected]>
26
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
27
class Check extends \ZendDiagnostics\Check\AbstractCheck implements CheckInterface
28
{
29
    use CheckTrait;
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
     * @var Expression|string
33
     */
34
    private $warningExpression;
0 ignored issues
show
Coding Style introduced by
Private member variable "warningExpression" must be prefixed with an underscore
Loading history...
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @var Expression|string
38
     */
39
    private $criticalExpression;
0 ignored issues
show
Coding Style introduced by
Private member variable "criticalExpression" must be prefixed with an underscore
Loading history...
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @var ?string
43
     */
44
    private $warningMessage;
0 ignored issues
show
Coding Style introduced by
Private member variable "warningMessage" must be prefixed with an underscore
Loading history...
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @var ?string
48
     */
49
    private $criticalMessage;
0 ignored issues
show
Coding Style introduced by
Private member variable "criticalMessage" must be prefixed with an underscore
Loading history...
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @param Expression|string $warningExpression
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
53
     * @param Expression|string $criticalExpression
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     * @param ?string           $warningMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
     * @param ?string           $criticalMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     *
57
     * @throws \Exception
58
     */
59 6
    public function __construct($warningExpression = null, $criticalExpression = null, $warningMessage = null, $criticalMessage = null)
60
    {
61 6
        if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
62
            throw new FeatureRequired('The symfony/expression-language is required for this check.');
63
        }
64
65 6
        if (!$warningExpression && !$criticalExpression) {
66 1
            throw new \InvalidArgumentException('Not checks set.');
67
        }
68
69 5
        $this->warningExpression = $warningExpression;
70 5
        $this->warningMessage = $warningMessage;
71 5
        $this->criticalExpression = $criticalExpression;
72 5
        $this->criticalMessage = $criticalMessage;
73 5
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78 5
    public function check()
79
    {
80 5
        $language = $this->getExpressionLanguage();
81
82 5
        if ($this->criticalExpression && false === $language->evaluate($this->criticalExpression)) {
0 ignored issues
show
introduced by
The condition false === $language->eva...is->criticalExpression) is always false.
Loading history...
83 3
            return new Failure($this->criticalMessage);
84
        }
85
86 3
        if ($this->warningExpression && false === $language->evaluate($this->warningExpression)) {
0 ignored issues
show
introduced by
The condition false === $language->eva...his->warningExpression) is always false.
Loading history...
87 1
            return new Warning($this->warningMessage);
88
        }
89
90 2
        return new Success();
91
    }
92
93 5
    protected function getExpressionLanguage()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getExpressionLanguage()
Loading history...
94
    {
95 5
        $language = new ExpressionLanguage();
96
        $language->register('ini', static function ($value) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
97
            return $value;
98
        }, static function ($arguments, $value) {
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 8.
Loading history...
99 1
            return ini_get($value);
100 5
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
101
102 5
        return $language;
103
    }
104
}
105