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

HttpsCertificateExpireRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 5
c 3
b 1
f 2
lcom 1
cbo 2
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A doValidate() 0 15 4
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