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

PhpExtensionCheck::checkStatus()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
nc 3
cc 6
nop 0
1
<?php
2
3
namespace BretRZaun\StatusPage\Check;
4
5
use BretRZaun\StatusPage\Result;
6
7
class PhpExtensionCheck extends AbstractCheck
8
{
9
    /** @var string */
10
    protected $extension;
11
12
    /** @var string */
13
    protected $greaterEquals;
14
15
    /** @var string */
16
    protected $lessThan;
17
18
    /**
19
     * @param string $label         Label
20
     * @param string $extension     extension to be tested
21
     * @param string $greaterEquals minimum version (optional)
22
     * @param string $lessThan      version has to be smaller than this (optional)
23
     */
24
    public function __construct(string $label, string $extension, string $greaterEquals = null, string $lessThan = null)
25
    {
26
        parent::__construct($label);
27
        $this->extension = $extension;
28
        $this->greaterEquals = $greaterEquals;
29
        $this->lessThan = $lessThan;
30
    }
31
32
    /**
33
     * Gets version constraint string for display within the error message.
34
     *
35
     * @return string
36
     */
37
    protected function getVersionConstraintString(): string
38
    {
39
        $strings = [];
40
        if (null !== $this->greaterEquals) {
41
            $strings[] = '>=' . $this->greaterEquals;
42
        }
43
        if (null !== $this->lessThan) {
44
            $strings[] = '<' . $this->lessThan;
45
        }
46
47
        return implode(' and ', $strings);
48
    }
49
50
    /**
51
     * Finds out whether the extension is loaded.
52
     *
53
     * @return bool
54
     */
55
    protected function isExtensionLoaded(): bool
56
    {
57
        return \extension_loaded($this->extension);
58
    }
59
60
    /**
61
     * Gets the installed extension version.
62
     *
63
     * @return string
64
     */
65
    protected function getExtensionVersion(): string
66
    {
67
        return phpversion($this->extension);
68
    }
69
70
    /**
71
     * Check URL
72
     *
73
     * @return Result
74
     */
75
    public function checkStatus(): Result
76
    {
77
        $result = new Result($this->label);
78
        if ($this->isExtensionLoaded()) {
79
            $version = $this->getExtensionVersion();
80
            if ((null !== $this->greaterEquals && !version_compare($this->greaterEquals, $version, '<=')) ||
81
                (null !== $this->lessThan && !version_compare($this->lessThan, $version, '>'))) {
82
                $result->setSuccess(false);
83
                $result->setError('Extension ' . $this->extension . ' loaded, but version ' . $version . ' not supported<br/>Version must be ' . $this->getVersionConstraintString());
84
            }
85
        } else {
86
            $result->setSuccess(false);
87
            $result->setError('Extension ' . $this->extension . ' is missing');
88
        }
89
90
        return $result;
91
    }
92
}
93