Completed
Push — master ( 3ccb4d...f0ec0e )
by Vladimir
05:12
created

Check   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 9
eloc 24
dl 0
loc 72
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getExpressionLanguage() 0 10 1
A check() 0 13 5
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 ZendDiagnostics\Result\Success;
17
use ZendDiagnostics\Result\Warning;
18
use ZendDiagnostics\Result\Failure;
19
20
use Tvi\MonitorBundle\Check\CheckInterface;
21
use Tvi\MonitorBundle\Check\CheckTrait;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author Vladimir Turnaev <[email protected]>
25
 */
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...
26
class Check extends \ZendDiagnostics\Check\AbstractCheck implements CheckInterface
27
{
28
    use CheckTrait;
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @var Expression|string
32
     */
33
    private $warningExpression;
0 ignored issues
show
Coding Style introduced by
Private member variable "warningExpression" must be prefixed with an underscore
Loading history...
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var Expression|string
37
     */
38
    private $criticalExpression;
0 ignored issues
show
Coding Style introduced by
Private member variable "criticalExpression" must be prefixed with an underscore
Loading history...
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var ?string
42
     */
43
    private $warningMessage;
0 ignored issues
show
Coding Style introduced by
Private member variable "warningMessage" must be prefixed with an underscore
Loading history...
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var ?string
47
     */
48
    private $criticalMessage;
0 ignored issues
show
Coding Style introduced by
Private member variable "criticalMessage" must be prefixed with an underscore
Loading history...
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @param Expression|string $warningExpression
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
52
     * @param Expression|string $criticalExpression
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
53
     * @param ?string           $warningMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     * @param ?string           $criticalMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
     *
56
     * @throws \Exception
57
     */
58 6
    public function __construct($warningExpression = null, $criticalExpression = null, $warningMessage = null, $criticalMessage = null)
59
    {
60 6
        if (!$warningExpression && !$criticalExpression) {
61 1
            throw new \InvalidArgumentException('Not checks set.');
62
        }
63
64 5
        $this->warningExpression = $warningExpression;
65 5
        $this->warningMessage = $warningMessage;
66 5
        $this->criticalExpression = $criticalExpression;
67 5
        $this->criticalMessage = $criticalMessage;
68 5
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73 5
    public function check()
74
    {
75 5
        $language = $this->getExpressionLanguage();
76
77 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...
78 3
            return new Failure($this->criticalMessage);
79
        }
80
81 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...
82 1
            return new Warning($this->warningMessage);
83
        }
84
85 2
        return new Success();
86
    }
87
88 5
    protected function getExpressionLanguage()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getExpressionLanguage()
Loading history...
89
    {
90 5
        $language = new ExpressionLanguage();
91
        $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...
92
            return $value;
93
        }, 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...
94 1
            return ini_get($value);
95 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...
96
97 5
        return $language;
98
    }
99
}
100