Passed
Pull Request — master (#661)
by
unknown
06:25
created

JsonValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSchema() 0 4 1
A getContentType() 0 2 1
A validate() 0 16 3
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq;
4
5
use Swaggest\JsonSchema\Context;
6
use Swaggest\JsonSchema\Schema;
7
use Swaggest\JsonSchema\Exception;
8
use Swaggest\JsonSchema\RemoteRef\Preloaded;
9
10
class JsonValidator implements ValidatorInterface
11
{
12
    public $definitions = null;
13
    public $schema = null;
14
    public $schema_url = null;
15
16
    public function setSchema($schema, $schema_url=null, $definitions=null) {
17
        $this->definitions = $definitions;
18
        $this->schema = $schema;
19
        $this->schema_url = $schema_url;
20
    }
21
22
23
    public function validate($msg)
24
    {
25
        try{
26
            $options = new Context();
27
28
            if ($this->definitions != null) {
29
                $refProvider = new Preloaded();
30
                $refProvider->setSchemaData($this->schema_url, json_decode(file_get_contents($this->definitions, true)));
31
                $options->remoteRefProvider = $refProvider;
32
            }
33
34
            $schema = Schema::import(json_decode(file_get_contents($this->schema, true)), $options);
35
            $schema->in($msg);
36
            return null;
37
        }catch (Exception $e){
38
            return $e->getMessage();
39
        }
40
    }
41
42
    public function getContentType() {
43
        return "application/json";
44
    }
45
}
46