Completed
Push — master ( 0aa8ac...a01137 )
by Bret R.
01:24
created

PhpMemoryLimitCheck::checkStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
nc 2
cc 2
nop 0
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
class PhpMemoryLimitCheck extends AbstractCheck
7
{
8
9
    /**
10
     * @var string
11
     */
12
    protected $memoryRequired;
13
14
    /**
15
     *
16
     * @param string $label Label
17
     * @param string $memoryRequired amount of memory that is (at least) required in megabytes
18
     */
19
    public function __construct(string $label, string $memoryRequired)
20
    {
21
        parent::__construct($label);
22
        $this->memoryRequired = $memoryRequired;
23
    }
24
25
    /**
26
     * Returns size in megabytes from a PHP size string like '1024M'.
27
     *
28
     * @param $sizeStr
29
     * @return float|int
30
     */
31
    public function getMegabytesFromSizeString($sizeStr)
32
    {
33
        switch (substr($sizeStr, -1)) {
34
            case 'M':
35
            case 'm':
36
                return (int)$sizeStr;
37
38
            case 'K':
39
            case 'k':
40
                return (int)$sizeStr / 1024;
41
42
            case 'G':
43
            case 'g':
44
                return (int)$sizeStr * 1024;
45
46
            default:
47
                return $sizeStr;
48
        }
49
    }
50
51
    /**
52
     * Check URL
53
     *
54
     * @return Result
55
     */
56
    public function checkStatus(): Result
57
    {
58
        $result = new Result($this->label);
59
        $memoryLimitString = ini_get('memory_limit');
60
        $memoryLimit = $this->getMegabytesFromSizeString($memoryLimitString);
61
62
        if ($this->memoryRequired > $memoryLimit) {
63
            $result->setSuccess(false);
64
            $result->setError("Memory required: {$this->memoryRequired}M; limit: {$memoryLimitString}");
65
        }
66
67
        return $result;
68
    }
69
}
70