PhpIniCheck::checkString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
class PhpIniCheck extends AbstractCheck
7
{
8
9
    const TypeBoolean = 'boolean';
10
    const TypeMemory  = 'memory';
11
    const TypeNumber  = 'number';
12
    const TypeRegex   = 'regex';
13
    const TypeString  = 'string';
14
15
    protected $varName;
16
    protected $varType;
17
    protected $varValue;
18
    protected $maxValue;
19
    protected $iniValue;
20
21
    /**
22
     * PhpIniCheck constructor.
23
     *
24
     * @param string $label
25
     * @param string $varName <b>Name</b> of ini value - used for ini_get(...)
26
     * @param string $varType <b>Type</b> of ini value - see class constant
27
     * @param mixed $varValue <b>Value</b> which is expected
28
     * @param null $maxValue <b>maximum</b> value is optional used for numbers
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $maxValue is correct as it would always require null to be passed?
Loading history...
29
     *
30
     * @return PhpIniCheck
31
     */
32
    public function __construct(string $label, string $varName, string $varType, $varValue, $maxValue = null)
33
    {
34
        parent::__construct($label);
35
        $this->varName = $varName;
36
        $this->varType = $varType;
37
        $this->varValue = $varValue;
38
        $this->maxValue = $maxValue;
39
        $this->iniValue = ini_get($varName);
40
    }
41
42
    /**
43
     * @param string $size
44
     * @return int|bool(false)
45
     */
46
    protected function stringToMegabyte(string $size)
47
    {
48
        $value = preg_replace('~[^0-9]*~', '', $size);
49
        switch (substr(strtolower($size), -1)) {
50
            case 'm':
51
                return (int) $value;
52
            case 'k':
53
                return (int) round((int) $value / 1024);
54
            case 'g':
55
                return ((int) $value * 1024);
56
            default:
57
                return false;
58
        }
59
    }
60
61
62
    /**
63
     * @return Result
64
     */
65
    public function checkStatus(): Result
66
    {
67
        switch ($this->varType) {
68
            case self::TypeBoolean:
69
                return $this->checkBoolean();
70
            case self::TypeMemory:
71
                return $this->checkMemory();
72
            case self::TypeNumber:
73
                return $this->checkNumber();
74
            case self::TypeRegex:
75
                return $this->checkRegex();
76
            case self::TypeString:
77
                return $this->checkString();
78
            default:
79
                $result = new Result($this->label);
80
                $result->setError("Invalid Type: ".$this->varType);
81
                return $result;
82
        }
83
    }
84
85
    /**
86
     * @return Result
87
     */
88
    protected function checkBoolean()
89
    {
90
        $result = new Result($this->label);
91
        // some boolval advance
92
        switch (strtolower($this->iniValue)) {
93
            case 'on':
94
                $this->iniValue = true;
95
                break;
96
            case 'off':
97
                $this->iniValue = false;
98
                break;
99
            case 'yes':
100
                $this->iniValue = true;
101
                break;
102
            case 'no':
103
                $this->iniValue = false;
104
                break;
105
        }
106
107
        if (boolval($this->iniValue) !== boolval($this->varValue)) {
108
            $result->setError("php.ini value of '".$this->varName."' is set to '".strval(boolval($this->iniValue))."' instead of expected '".strval(boolval($this->varValue))."'");
109
        }
110
        return $result;
111
    }
112
113
    /**
114
     * @return Result
115
     */
116
    protected function checkMemory()
117
    {
118
        $result = new Result($this->label);
119
        if ($this->iniValue != -1) {
120
            $value = $this->stringToMegabyte($this->iniValue);
121
            if ($value < $this->varValue) {
122
                $result->setError(
123
                    "php.ini value of '".$this->varName."' is set to '".
124
                    strval($this->iniValue)."', minimum expected is '".
125
                    strval($this->varValue)."'"
126
                );
127
            }
128
        }
129
        return $result;
130
    }
131
132
    /**
133
     * @return Result
134
     */
135
    protected function checkNumber()
136
    {
137
        $result = new Result($this->label);
138
        if (!is_null($this->varValue)) {
139
            if ($this->iniValue < $this->varValue) {
140
                $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', minimum expected is '".strval($this->varValue)."'");
141
                return $result;
142
            }
143
        }
144
        if (!is_null($this->maxValue)) {
145
            if ($this->iniValue > $this->maxValue) {
146
                $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', maximum expected is '".strval($this->maxValue)."'");
147
                return $result;
148
            }
149
        }
150
        $result->setDetails("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."'");
151
        return $result;
152
    }
153
154
    /**
155
     * @return Result
156
     */
157
    protected function checkRegex()
158
    {
159
        $result = new Result($this->label);
160
        if (!preg_match('~'.$this->varValue.'~', $this->iniValue)) {
161
            $result->setError(
162
                "php.ini value of '".$this->varName."' is set to '".
163
                strval($this->iniValue)."', expected is '".
164
                strval($this->varValue)."'"
165
            );
166
        }
167
        return $result;
168
    }
169
170
    /**
171
     * @return Result
172
     */
173
    protected function checkString()
174
    {
175
        $result = new Result($this->label);
176
        if (strval($this->iniValue) != strval($this->varValue)) {
177
            $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', expected is '".strval($this->varValue)."'");
178
        }
179
        return $result;
180
    }
181
}
182