GZipRule::doValidation()   B
last analyzed

Complexity

Conditions 9
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.0555
c 0
b 0
f 0
cc 9
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header;
4
5
use phm\HttpWebdriverClient\Http\Response\ContentTypeAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
7
use whm\Smoke\Rules\StandardRule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
/**
11
 * This rule checks if gzip compressions is activated.
12
 */
13
class GZipRule extends StandardRule
14
{
15
    private $minFileSize;
16
17
    public function init($minFileSize = 200)
18
    {
19
        $this->minFileSize = $minFileSize;
20
    }
21
22
    public function doValidation(ResponseInterface $response)
23
    {
24
        if ($response instanceof ContentTypeAwareResponse) {
25
            if (strpos($response->getContentType(), 'image') === false
26
                && strpos($response->getContentType(), 'pdf') === false
27
                && strpos($response->getContentType(), 'postscript') === false
28
                && strpos($response->getContentType(), 'zip') === false
29
                && strlen((string)$response->getBody()) >= $this->minFileSize
30
            ) {
31
                if (!$response->hasHeader('Content-Encoding') || $response->getHeader('Content-Encoding')[0] !== 'gzip') {
32
                    throw new ValidationFailedException('gzip compression not active');
33
                }
34
            }
35
        }
36
    }
37
}
38