Passed
Push — master ( c5502b...385fa8 )
by Radu
04:32
created

Response::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Api\JsonApi;
3
4
class Response extends \WebServCo\Framework\HttpResponse
5
{
6
    public function __construct(
7
        \WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface $resourceObject,
8
        $errors = [],
9
        $statusCode = 200
10
    ) {
11
        $structure = new Structure();
12
        $structure->setData($resourceObject);
13
        foreach ($errors as $error) {
14
            if ($error instanceof \WebServCo\Api\JsonApi\Error) {
15
                $structure->setError($error);
16
            }
17
        }
18
19
        parent::__construct($this->getOutput($structure), $statusCode, ['Content-Type' => Structure::CONTENT_TYPE]);
20
    }
21
22
    protected function getOutput(Structure $structure)
23
    {
24
        $jsonOutput = new \WebServCo\Framework\Libraries\JsonOutput();
25
        $array = $structure->toArray();
26
        foreach ($array as $key => $value) {
27
            $jsonOutput->setData($key, $value);
28
        }
29
        return $jsonOutput->render();
30
    }
31
}
32