Completed
Push — master ( 39ac74...65bbcd )
by Nils
27:12 queued 08:28
created

GZipRule::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\Rule;
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(Response $response)
23
    {
24
        if (strpos($response->getContentType(), 'image') === false
25
            && strpos($response->getContentType(), 'pdf') === false
26
            && strlen((string)$response->getBody()) >= $this->minFileSize
27
        ) {
28
            if (!$response->hasHeader('Content-Encoding') || $response->getHeader('Content-Encoding')[0] !== 'gzip') {
29
                throw new ValidationFailedException('gzip compression not active');
30
            }
31
        }
32
    }
33
}
34