Completed
Push — master ( 84aa2d...183b20 )
by Nils
20:22
created

HttpsCertificateExpireRule::validate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 29
rs 8.439
cc 5
eloc 20
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http;
4
5
use whm\Smoke\Rules\ValidationFailedException;
6
7
/**
8
 * This rule checks if a given https certificate expire in a few days.
9
 */
10
class HttpsCertificateExpireRule extends HttpsRule
11
{
12
    private $expireWarningTime;
13
14
    /**
15
     * @param int $expireWarningTime in days
16
     */
17
    public function init($expireWarningTime = 14)
18
    {
19
        $this->expireWarningTime = $expireWarningTime;
20
    }
21
22
    protected function doValidate($certInfo)
23
    {
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
            throw new ValidationFailedException($errorMessage);
31
        } elseif ($certInfo['validTo_time_t'] < strtotime('+' . $this->expireWarningTime . 'days')) {
32
            $errorMessage = 'Certificate warning, expires in less than ' . $this->expireWarningTime . ' days. Certificate expires at: ' . $validTo;
33
            throw new ValidationFailedException($errorMessage);
34
        }
35
36
    }
37
}
38