Completed
Branch master (1f9106)
by Nils
02:44
created

ValidRule   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

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