InstagramResponse   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 178
c 0
b 0
f 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setParams() 0 8 1
A extractBodyParts() 0 14 3
A getBody() 0 4 1
A getStatusCode() 0 4 1
A getHeader() 0 4 2
A getHeaders() 0 4 1
A getData() 0 4 1
A getMetaData() 0 4 1
A getPagination() 0 4 1
A isMetaDataSet() 0 4 1
A isPaginationSet() 0 4 1
A getProtocol() 0 4 1
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Haridarshan Gorana
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
namespace Haridarshan\Instagram;
26
27
use GuzzleHttp\Psr7\Response;
28
use Haridarshan\Instagram\Exceptions\InstagramResponseException;
29
30
class InstagramResponse
31
{
32
    /** @var int $status_code */
33
    protected $statusCode;
34
35
    /** @var string $protocol */
36
    protected $protocol = '1.1';
37
38
    /** @var array $headers */
39
    protected $headers = [];
40
41
    /** @var bool */
42
    protected $isPagination = false;
43
44
    /** @var object */
45
    protected $pagination;
46
47
    /** @var bool */
48
    protected $isMetaData = false;
49
50
    /** @var object */
51
    protected $metaData;
52
53
    /** @var object */
54
    protected $data;
55
56
    /** @var object $body */
57
    protected $body;
58
59
    /**
60
     * InstagramResponse Entity
61
     *
62
     * @param Response $response
63
     *
64
     * @throws InstagramResponseException
65
     */
66
    public function __construct(Response $response)
67
    {
68
        if (!$response instanceof Response) {
69
            throw new InstagramResponseException('Bad Request: Response is not valid instance of GuzzleHttp\Psr7\Response', 404);
70
        }
71
        $this->setParams($response);
72
    }
73
74
    /**
75
     * Set Values to the class members
76
     *
77
     * @param Response $response
78
     */
79
    private function setParams(Response $response)
80
    {
81
        $this->protocol = $response->getProtocolVersion();
82
        $this->statusCode = (int) $response->getStatusCode();
83
        $this->headers = $response->getHeaders();
84
        $this->body = json_decode($response->getBody()->getContents());
85
        $this->extractBodyParts();
86
    }
87
88
    /**
89
     * Extract Body Parts from the response
90
     */
91
    private function extractBodyParts()
92
    {
93
        if (isset($this->body->pagination)) {
94
            $this->isPagination = true;
95
            $this->pagination = $this->body->pagination;
96
        }
97
98
        if (isset($this->body->meta)) {
99
            $this->isMetaData = true;
100
            $this->metaData = $this->body->meta;
101
        }
102
103
        $this->data = $this->body->data;
104
    }
105
106
    /**
107
     * Get response
108
     *
109
     * @return object|string
110
     */
111
    public function getBody()
112
    {
113
        return $this->body;
114
    }
115
116
    /**
117
     * Get Status Code
118
     *
119
     * @return int
120
     */
121
    public function getStatusCode()
122
    {
123
        return $this->statusCode;
124
    }
125
126
    /**
127
     * Get specific header
128
     *
129
     * @param string $header
130
     *
131
     * @retrun string
132
     */
133
    public function getHeader($header)
134
    {
135
        return isset($this->headers[$header]) ? $this->headers[$header] : [];
136
    }
137
138
    /**
139
     * Get all headers
140
     *
141
     * @retrun array
142
     */
143
    public function getHeaders()
144
    {
145
        return $this->headers;
146
    }
147
148
    /**
149
     * Get data from body
150
     *
151
     * @return object
152
     */
153
    public function getData()
154
    {
155
        return $this->data;
156
    }
157
158
    /**
159
     * Get Meta data
160
     *
161
     * @return object
162
     */
163
    public function getMetaData()
164
    {
165
        return $this->metaData;
166
    }
167
168
    /**
169
     * Get Meta data
170
     *
171
     * @return object
172
     */
173
    public function getPagination()
174
    {
175
        return $this->pagination;
176
    }
177
178
    /**
179
     * Is Meta Data Present
180
     *
181
     * @return bool
182
     */
183
    public function isMetaDataSet()
184
    {
185
        return $this->isMetaData;
186
    }
187
188
    /**
189
     * Is Pagination present
190
     *
191
     * @return bool
192
     */
193
    public function isPaginationSet()
194
    {
195
        return $this->isPagination;
196
    }
197
198
    /**
199
     * Get Protocol version
200
     *
201
     * @return string
202
     */
203
    public function getProtocol()
204
    {
205
        return $this->protocol;
206
    }
207
}
208