Completed
Pull Request — master (#55)
by Michal
06:03
created

JsonOutput   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A validate() 0 13 3
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