ResponseBag::getContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 2018/6/15
6
 * Time: 23:30
7
 */
8
9
namespace TimSDK\Foundation;
10
11
use TimSDK\Core\Exceptions\JsonParseException;
12
use TimSDK\Support\Collection;
13
use TimSDK\Support\Json;
14
15
/**
16
 * Class ResponseBag
17
 * @package TimSDK\Foundation
18
 */
19
class ResponseBag
20
{
21
    /**
22
     * @var Collection
23
     */
24
    protected $headers;
25
26
    /**
27
     * @var Collection
28
     */
29
    protected $contents;
30
31
    /**
32
     * Http status code
33
     *
34
     * @var int
35
     */
36
    protected $statusCode;
37
38 5
    public function __construct($contents, $headers, $statusCode = 200)
39
    {
40 5
        $this->contents = $this->getCollectionItems($contents);
41 5
        $this->headers = $this->getCollectionItems($headers);
42 5
        $this->statusCode = $statusCode;
43 5
    }
44
45
    /**
46
     * @return int
47
     */
48 1
    public function getStatusCode()
49
    {
50 1
        return $this->statusCode;
51
    }
52
53
    /**
54
     * @return Collection
55
     */
56 1
    public function getHeaders()
57
    {
58 1
        return $this->headers;
59
    }
60
61
    /**
62
     * @return Collection
63
     */
64 2
    public function getContents()
65
    {
66 2
        return $this->contents;
67
    }
68
69
    /**
70
     * Get a header parameter
71
     *
72
     * @param      $name
73
     * @param null $default
74
     * @return mixed
75
     */
76
    public function getHeader($name, $default = null)
77
    {
78
        return $this->headers->get($name, $default);
79
    }
80
81
    /**
82
     * Get a contents parameter
83
     *
84
     * @param      $name
85
     * @param null $default
86
     * @return mixed
87
     */
88 1
    public function getContent($name, $default = null)
89
    {
90 1
        return $this->contents->get($name, $default);
91
    }
92
93
    /**
94
     * Results array of items from Collection or Arrayable.
95
     *
96
     * @param $items
97
     * @return Collection
98
     */
99 5
    protected function getCollectionItems($items)
100
    {
101 5
        if ($items instanceof Collection) {
102 1
            return $items;
103 5
        } elseif ($items instanceof \JsonSerializable) {
104 1
            $items = $items->jsonSerialize();
105 5
        } elseif (is_string($items)) {
106
            try {
107 2
                $items = Json::decode($items, true);
108 1
            } catch (JsonParseException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
109
            }
110
        }
111
112 5
        return new Collection((array) $items);
113
    }
114
}
115