Check::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 4
crap 3
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 JMS\Serializer\Annotation as JMS;
17
use ZendDiagnostics\Result\Success;
18
use ZendDiagnostics\Result\Warning;
19
use ZendDiagnostics\Result\Failure;
20
use Tvi\MonitorBundle\Check\CheckAbstract;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @JMS\ExclusionPolicy("all")
24
 *
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 CheckAbstract
28
{
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var Expression|string
31
     */
32
    private $warningExpression;
0 ignored issues
show
Coding Style introduced by
Private member variable "warningExpression" must be prefixed with an underscore
Loading history...
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @var Expression|string
36
     */
37
    private $criticalExpression;
0 ignored issues
show
Coding Style introduced by
Private member variable "criticalExpression" must be prefixed with an underscore
Loading history...
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
40
     * @var ?string
41
     */
42
    private $warningMessage;
0 ignored issues
show
Coding Style introduced by
Private member variable "warningMessage" must be prefixed with an underscore
Loading history...
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var ?string
46
     */
47
    private $criticalMessage;
0 ignored issues
show
Coding Style introduced by
Private member variable "criticalMessage" must be prefixed with an underscore
Loading history...
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @param Expression|string $warningExpression
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     * @param Expression|string $criticalExpression
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
52
     * @param ?string           $warningMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
53
     * @param ?string           $criticalMessage
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     *
55
     * @throws \Exception
56
     */
57 6
    public function __construct($warningExpression = null, $criticalExpression = null, $warningMessage = null, $criticalMessage = null)
58
    {
59 6
        if (!$warningExpression && !$criticalExpression) {
60 1
            throw new \InvalidArgumentException('Not checks set.');
61
        }
62
63 5
        $this->warningExpression = $warningExpression;
64 5
        $this->warningMessage = $warningMessage;
65 5
        $this->criticalExpression = $criticalExpression;
66 5
        $this->criticalMessage = $criticalMessage;
67 5
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
72 5
    public function check()
73
    {
74 5
        $language = $this->getExpressionLanguage();
75
76 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...
77 3
            return new Failure($this->criticalMessage);
78
        }
79
80 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...
81 1
            return new Warning($this->warningMessage);
82
        }
83
84 2
        return new Success();
85
    }
86
87 5
    protected function getExpressionLanguage()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getExpressionLanguage()
Loading history...
88
    {
89 5
        $language = new ExpressionLanguage();
90
        $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...
91
            return $value;
92
        }, 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...
93 1
            return ini_get($value);
94 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...
95
96 5
        return $language;
97
    }
98
}
99