PragmaNoCacheRule::validate()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 12

Duplication

Lines 3
Ratio 25 %

Importance

Changes 0
Metric Value
dl 3
loc 12
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header\Cache;
4
5
use Psr\Http\Message\ResponseInterface;
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(ResponseInterface $response)
22
    {
23
        if ($response->getStatusCode() <= $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 View Code Duplication
            if ($response->hasHeader('Cache-Control') && false !== strpos($response->getHeader('Cache-Control')[0], 'no-cache')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
                throw new ValidationFailedException('cache-control:no-cache was found');
30
            }
31
        }
32
    }
33
}
34