Completed
Push — master ( 0aefd9...60733b )
by Nils
02:05
created

HttpsCertificateExpireRule::doValidate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 18
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http;
4
5
use whm\Smoke\Rules\Attribute;
6
use whm\Smoke\Rules\CheckResult;
7
8
/**
9
 * This rule checks if a given https certificate expire in a few days.
10
 */
11
class HttpsCertificateExpireRule extends HttpsRule
12
{
13
    private $expireWarningTime;
14
15
    /**
16
     * @param int $expireWarningTime in days
17
     */
18
    public function init($expireWarningTime = 14)
19
    {
20
        $this->expireWarningTime = $expireWarningTime;
21
    }
22
23
    protected function doValidate($certInfo)
24
    {
25
        $validFrom = date('d.m.Y H:i:s', $certInfo['validFrom_time_t']);
26
        $validTo = date('d.m.Y H:i:s', $certInfo['validTo_time_t']);
27
28
        if ($certInfo['validFrom_time_t'] > time() || $certInfo['validTo_time_t'] < time()) {
29
            $errorMessage = 'Certificate is expired. [' . $validFrom . ' - ' . $validTo . ']';
30
31
            $result = new CheckResult(CheckResult::STATUS_FAILURE, $errorMessage);
32
            $infoJson = json_encode($certInfo);
33
            if ($infoJson === false) {
34
                $result->addAttribute(new Attribute('json_encode error', json_last_error_msg(), false));
35
            } else {
36
                $result->addAttribute(new Attribute('certificate information', $infoJson, true));
37
            }
38
            return $result;
39
        } elseif ($certInfo['validTo_time_t'] < strtotime('+' . $this->expireWarningTime . 'days')) {
40
            $errorMessage = 'Certificate warning, expires in less than ' . $this->expireWarningTime . ' days. Certificate expires at: ' . $validTo;
41
42
            $result = new CheckResult(CheckResult::STATUS_FAILURE, $errorMessage);
43
            $result->addAttribute(new Attribute('certificate information', json_encode($certInfo), true));
44
            return $result;
45
        }
46
47
        return new CheckResult(CheckResult::STATUS_SUCCESS, 'The certificate does not expire within the next ' . $this->expireWarningTime . ' days. Expire date: ' . $validTo . '.');
48
    }
49
}
50