Response::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 3
nop 1
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/29/15
6
 * Time: 12:47 AM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Api\JsonApi\Http\Response;
12
13
/**
14
 * Class Response.
15
 */
16
class Response extends AbstractResponse
17
{
18
    /**
19
     * @var int
20
     */
21
    protected $httpCode = 200;
22
23
    /**
24
     * @var array
25
     */
26
    protected $links = [
27
        'next' => 'next',
28
        'last' => 'last',
29
        'first' => 'first',
30
        'previous' => 'prev',
31
    ];
32
33
    /**
34
     * @param string $json
35
     */
36
    public function __construct($json)
37
    {
38
        $pagination = json_decode($json, true);
39
        if (!empty($pagination['links'])) {
40
            $headerLinks = [];
41
            foreach ($this->links as $linkName => $relName) {
42
                if (!empty($pagination['links'][$linkName]['href'])) {
43
                    $headerLinks[] = sprintf('<%s>; rel="%s"', $pagination['links'][$linkName]['href'], $relName);
44
                }
45
            }
46
47
            if (!empty($headerLinks)) {
48
                $this->headers['Link'] = implode(', ', $headerLinks);
49
            }
50
        }
51
52
        parent::__construct($json);
53
    }
54
}
55