Completed
Push — master ( 46fde5...13f008 )
by Nils
05:11
created

ExistsRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A doValidation() 0 14 4
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\StandardRule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule checks if gzip compressions is activated.
11
 */
12
class ExistsRule extends StandardRule
13
{
14
    private $checkedHeaders;
15
16
    public function init(array $checkedHeaders)
17
    {
18
        $this->checkedHeaders = $checkedHeaders;
19
    }
20
21
    public function doValidation(Response $response)
22
    {
23
        foreach ($this->checkedHeaders as $headerConfig) {
24
            if (!$response->hasHeader($headerConfig['key'])) {
25
                throw new ValidationFailedException('Header not found (' . $headerConfig['key'] . ')');
26
            }
27
28
            $currentValue = $response->getHeader($headerConfig['key'])[0];
29
30
            if ($currentValue != $headerConfig['value']) {
31
                throw new ValidationFailedException('Header "' . $headerConfig['key'] . '" does not equal "' . $headerConfig['value'] . '". Current value is "' . $currentValue . '"');
32
            }
33
        }
34
    }
35
}
36