ExistsRule   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A doValidation() 0 16 4
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header;
4
5
use Psr\Http\Message\ResponseInterface;
6
use whm\Smoke\Http\Response;
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 ExistsRule extends StandardRule
14
{
15
    private $checkedHeaders;
16
17
    public function init(array $checkedHeaders)
18
    {
19
        $this->checkedHeaders = $checkedHeaders;
20
    }
21
22
    public function doValidation(ResponseInterface $response)
23
    {
24
        // @todo the test should not fail with the first not found header
25
26
        foreach ($this->checkedHeaders as $headerConfig) {
27
            if (!$response->hasHeader($headerConfig['key'])) {
28
                throw new ValidationFailedException('Header not found (' . $headerConfig['key'] . ')');
29
            }
30
31
            $currentValue = $response->getHeader($headerConfig['key'])[0];
32
33
            if (!preg_match('%' . $headerConfig['value'] . '%', $currentValue, $matches)) {
34
                throw new ValidationFailedException('Header "' . $headerConfig['key'] . '" does not match "' . $headerConfig['value'] . '". Current value is "' . $currentValue . '"');
35
            }
36
        }
37
    }
38
}
39