Completed
Pull Request — master (#56)
by Michal
12:52
created

JsonOutput   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 47
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A validate() 0 13 3
A getCode() 0 4 1
A getSchema() 0 4 1
A getDescription() 0 4 1
1
<?php
2
3
namespace Tomaj\NetteApi\Output;
4
5
use JsonSchema\Validator;
6
use Tomaj\NetteApi\Response\JsonApiResponse;
7
8
class JsonOutput implements OutputInterface
9
{
10
    private $schemaValidator;
11
12
    private $code;
13
14
    private $schema;
15
16
    private $description;
17
18
    public function __construct($code, $schema, $description = null)
19
    {
20
        $this->schemaValidator = new Validator();
21
        $this->code = $code;
22
        $this->schema = $schema;
23
        $this->description = $description;
24
    }
25
26
    public function validate($response)
27
    {
28
        if (!$response instanceof JsonApiResponse) {
29
            return false;
30
        }
31
32
        if ($this->code !== $response->getCode()) {
33
            return false;
34
        }
35
        $value = json_decode(json_encode($response->getPayload()));
36
        $this->schemaValidator->validate($value, json_decode($this->schema));
37
        return $this->schemaValidator->isValid();
38
    }
39
40
    public function getCode()
41
    {
42
        return $this->code;
43
    }
44
45
    public function getSchema()
46
    {
47
        return $this->schema;
48
    }
49
50
    public function getDescription()
51
    {
52
        return $this->description;
53
    }
54
}
55