StandardRule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
doValidation() 0 1 ?
A validate() 0 19 5
A assert() 0 6 2
1
<?php
2
3
namespace whm\Smoke\Rules;
4
5
use phm\HttpWebdriverClient\Http\Response\ContentTypeAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
7
8
abstract class StandardRule implements Rule
9
{
10
    protected $contentTypes = array();
11
12
    public function validate(ResponseInterface $response)
13
    {
14
        /** @var ContentTypeAwareResponse $response */
15
16
        if (count($this->contentTypes) > 0) {
17
            $valid = false;
18
            foreach ($this->contentTypes as $validContentType) {
19
                if (strpos(strtolower($response->getContentType()), strtolower($validContentType)) !== false) {
20
                    $valid = true;
21
                    break;
22
                }
23
            }
24
            if (!$valid) {
25
                return;
26
            }
27
        }
28
29
        return $this->doValidation($response);
30
    }
31
32
    abstract protected function doValidation(ResponseInterface $response);
33
34
    /**
35
     * @param $valueToBeTrue
36
     * @param $errorMessage
37
     * @throws ValidationFailedException
38
     */
39
    protected function assert($valueToBeTrue, $errorMessage)
40
    {
41
        if (!$valueToBeTrue) {
42
            throw new ValidationFailedException($errorMessage);
43
        }
44
    }
45
}
46