JsonSchema   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 17 3
1
<?php
2
3
namespace Rezzza\RestApiBehatExtension\Json;
4
5
use JsonSchema\SchemaStorage;
6
use JsonSchema\Validator;
7
8
class JsonSchema
9
{
10
    private $filename;
11
12
    /**
13
     * @param string $filename
14
     */
15
    public function __construct($filename)
16
    {
17
        $this->filename = $filename;
18
    }
19
20
    public function validate(Json $json, Validator $validator, SchemaStorage $schemaStorage)
21
    {
22
        $schema = $schemaStorage->resolveRef('file://' . realpath($this->filename));
23
        $data = $json->getRawContent();
24
25
        $validator->check($data, $schema);
26
27
        if (!$validator->isValid()) {
28
            $msg = "JSON does not validate. Violations:" . PHP_EOL;
29
            foreach ($validator->getErrors() as $error) {
30
                $msg .= sprintf("  - [%s] %s" . PHP_EOL, $error['property'], $error['message']);
31
            }
32
            throw new \Exception($msg);
33
        }
34
35
        return true;
36
    }
37
}
38