Completed
Push — master ( 062b07...4cbe2f )
by Nils
02:41
created

PragmaNoCacheRule::validate()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 6
eloc 6
nc 4
nop 1
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 there are no "pragma: no-cache" or "cache-control: no-cache" header are set.
11
 */
12
class PragmaNoCacheRule 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('Pragma') && 'no-cache' === $response->getHeader('Pragma')[0]) {
25
                throw new ValidationFailedException('pragma:no-cache was found');
26
            }
27
28
            if ($response->hasHeader('Cache-Control') && false !== strpos($response->getHeader('Cache-Control')[0], 'no-cache')) {
29
                throw new ValidationFailedException('cache-control:no-cache was found');
30
            }
31
        }
32
    }
33
}
34