Completed
Push — master ( e54f24...78fdd6 )
by Nils
03:03
created

ValidRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A doValidation() 0 7 2
1
<?php
2
3
namespace whm\Smoke\Rules\Json;
4
5
use Psr\Http\Message\ResponseInterface;
6
use whm\Smoke\Http\Response;
7
use whm\Smoke\Rules\Rule;
8
use whm\Smoke\Rules\StandardRule;
9
use whm\Smoke\Rules\ValidationFailedException;
10
11
/**
12
 * This rule checks if a given json file is valid.
13
 */
14
class ValidRule extends StandardRule
15
{
16
    protected $contentTypes = ['json'];
17
18
    private $json_errors = array(
19
        JSON_ERROR_NONE => 'No Error',
20
        JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
21
        JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
22
        JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
23
        JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
24
        JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
25
    );
26
27
    public function init() {
28
        
29
    }
30
31
    public function doValidation(ResponseInterface $response)
32
    {
33
        $result = json_decode((string)$response->getBody());
34
        if ($result === null) {
35
            throw new ValidationFailedException("The given JSON data can not be validated (last error: '" . $this->json_errors[json_last_error()] . "').");
36
        }
37
    }
38
}
39