Passed
Pull Request — master (#661)
by
unknown
09:05
created

JsonValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSchema() 0 3 1
A isValid() 0 16 3
A getContentType() 0 2 1
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
15
    public function setSchema($schema, $definitions=null) {
16
        $this->definitions = $definitions;
17
        $this->schema = $schema;
18
    }
19
20
21
    public function isValid($msg)
22
    {
23
        try{
24
            $options = new Context();
25
26
            if ($this->definitions != null) {
27
                $refProvider = new Preloaded();
28
                $refProvider->setSchemaData("defs.schema", json_decode(file_get_contents("./defs.schema", true)));
29
                $options->remoteRefProvider = $refProvider;
30
            }
31
32
            $schema = Schema::import(json_decode(file_get_contents($this->schema, true)), $options);
33
            $schema->in($msg);
34
            return null;
35
        }catch (Exception $e){
36
            return $e->getMessage();
37
        }
38
    }
39
40
    public function getContentType() {
41
        return "application/json";
42
    }
43
}