Completed
Branch master (1f9106)
by Nils
03:23
created

ExpiresRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 24
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B validate() 0 14 5
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header\Cache;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\Rule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule checks if a expire header is in the past.
11
 */
12
class ExpiresRule implements Rule
13
{
14
    private $maxStatusCode;
15
16
    public function init($maxStatusCode = 200)
17
    {
18
        $this->maxStatusCode = $maxStatusCode;
19
    }
20
21
    public function validate(Response $response)
22
    {
23
        if ($response->getStatus() <= $this->maxStatusCode) {
24
            if ($response->hasHeader('Expires')) {
25
                $expireRaw = preg_replace('/[^A-Za-z0-9\-\/,]/', '', $response->getHeader('Expires')[0]);
26
                if ($expireRaw !== '') {
27
                    $expires = strtotime($response->getHeader('Expires')[0]);
28
                    if ($expires < time()) {
29
                        throw new ValidationFailedException('expires in the past');
30
                    }
31
                }
32
            }
33
        }
34
    }
35
}
36