JsonSchema::validateJsonSchema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 1
1
<?php
2
namespace PTS;
3
4
use FilesystemIterator;
5
use JsonSchema\Constraints\Factory;
6
use JsonSchema\SchemaStorage;
7
use JsonSchema\Validator;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
11
/**
12
 * Json schema module for codeception
13
 */
14
class JsonSchema
15
{
16
    protected $baseUri = 'http://demo.org';
17
18
    /** @var Validator */
19
    protected $validator;
20
    /** @var SchemaStorage */
21
    protected $schemaStorage;
22
23 7
    public function __construct()
24
    {
25 7
        $this->schemaStorage = new SchemaStorage;
26 7
        $this->validator = new Validator( new Factory($this->schemaStorage));
27 7
    }
28
29 1
    public function setBaseUri(string $baseUri = 'http://demo.org'): void
30
    {
31 1
        $this->baseUri = $baseUri;
32 1
    }
33
34 1
    public function getBaseUri(): string
35
    {
36 1
        return $this->baseUri;
37
    }
38
39 6
    public function loadAllSchemas(string $schemaDir): void
40
    {
41 6
        $schemaDir = $this->prepareDirPath($schemaDir);
42
43 6
        $iterator = new RecursiveDirectoryIterator($schemaDir, FilesystemIterator::SKIP_DOTS);
44 6
        foreach (new RecursiveIteratorIterator($iterator) as $path) {
45 6
            $relPath = str_replace($schemaDir, '', $path);
46 6
            $schema = json_decode(file_get_contents($path));
47 6
            $this->schemaStorage->addSchema($this->baseUri . $relPath, $schema);
48
        }
49 6
    }
50
51 6
    protected function prepareDirPath(string $schemaDir): string
52
    {
53 6
        if (substr($schemaDir, -1) !== DIRECTORY_SEPARATOR) {
54 1
            $schemaDir .= DIRECTORY_SEPARATOR;
55
        }
56
57 6
        return $schemaDir;
58
    }
59
60 6
    public function resolveSchemaByPath(string $path): \stdClass
61
    {
62 6
        return $this->schemaStorage->getSchema($this->baseUri . $path);
63
    }
64
65 3
    public function validateJsonSchema(string $response, string $pathSchema): ?string
66
    {
67 3
        $responseData = json_decode($response);
68 3
        $schema = $this->resolveSchemaByPath($pathSchema);
69
70 3
        $this->validator->reset();
71 3
        $this->validator->validate($responseData, $schema);
72 3
        $isValid = $this->validator->isValid();
73
74 3
        return $this->getErrorMessage($isValid);
75
    }
76
77 3
    protected function getErrorMessage(bool $isValid): ?string
78
    {
79 3
        if ($isValid) {
80 1
            return null;
81
        }
82
83 2
        $message = [];
84 2
        foreach ($this->validator->getErrors() as $error) {
85 2
            $message[] = sprintf("[%s] %s", $error['property'], $error['message']);
86
        }
87
88 2
        return implode('; ', $message);
89
    }
90
}
91