Completed
Push — master ( 315c47...bac1eb )
by Patrick
04:00 queued 02:13
created

MailxpertResponse::getLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sources.
4
 * Date: 14/08/15
5
 */
6
7
namespace Mailxpert;
8
9
use Mailxpert\Exceptions\MailxpertSDKException;
10
use Mailxpert\Model\ModelFactory;
11
12
/**
13
 * Class MailxpertResponse
14
 * @package Mailxpert
15
 */
16
class MailxpertResponse
17
{
18
    /**
19
     * @var MailxpertRequest
20
     */
21
    private $request;
22
23
    /**
24
     * @var string
25
     */
26
    private $body;
27
28
    /**
29
     * @var int
30
     */
31
    private $httpResponseCode;
32
33
    /**
34
     * @var array
35
     */
36
    private $headers;
37
38
    /**
39
     * @var array
40
     */
41
    protected $decodedBody = [];
42
43
    /**
44
     * @var MailxpertSDKException
45
     */
46
    protected $thrownException;
47
48
    /**
49
     * MailxpertResponse constructor.
50
     *
51
     * @param MailxpertRequest $request
52
     * @param string           $body
53
     * @param int              $httpResponseCode
54
     * @param array            $headers
55
     */
56
    public function __construct(MailxpertRequest $request, $body = null, $httpResponseCode = null, array $headers = [])
57
    {
58
        $this->request = $request;
59
        $this->body = $body;
60
        $this->httpResponseCode = $httpResponseCode;
61
        $this->headers = $headers;
62
63
        $this->decodeBody();
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function __toString()
70
    {
71
        return $this->body;
72
    }
73
74
    /**
75
     * @return MailxpertRequest
76
     */
77
    public function getRequest()
78
    {
79
        return $this->request;
80
    }
81
82
    /**
83
     * @return MailxpertApp
84
     */
85
    public function getApp()
86
    {
87
        return $this->request->getApp();
88
    }
89
90
    /**
91
     * @return string|null
92
     */
93
    public function getAccessToken()
94
    {
95
        return $this->request->getAccessToken();
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getBody()
102
    {
103
        return $this->body;
104
    }
105
106
    /**
107
     * @return int
108
     */
109
    public function getHttpResponseCode()
110
    {
111
        return $this->httpResponseCode;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function isHttpResponseCodeOK()
118
    {
119
        return (substr($this->httpResponseCode, 0, 1) == 2);
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function getHeaders()
126
    {
127
        return $this->headers;
128
    }
129
130
    /**
131
     * @param string $header
132
     *
133
     * @return string|null
134
     */
135
    public function getHeader($header)
136
    {
137
        return isset($this->headers[$header]) ? $this->headers[$header] : null;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getLocation()
144
    {
145
        return $this->getHeader('Location');
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getLocationId()
152
    {
153
        preg_match('/\/(\d+)$/', $this->getLocation(), $matches);
154
155
        return (int) $matches[1];
156
    }
157
158
    /**
159
     * Decode body
160
     */
161
    public function decodeBody()
162
    {
163
        $decodedBody = json_decode($this->body, true);
164
165
        if (is_array($decodedBody)) {
166
            $this->decodedBody = $decodedBody;
167
        } elseif (is_numeric($decodedBody)) {
168
            $this->decodedBody = ['id' => $this->decodedBody];
169
        } elseif (is_null($decodedBody)) {
170
            $this->decodedBody = [];
171
            parse_str($this->body, $this->decodedBody);
172
        } else {
173
            $this->decodedBody = [];
174
        }
175
176
        if ($this->isError()) {
177
            $this->makeException();
178
        }
179
    }
180
181
    /**
182
     * @return array
183
     */
184
    public function getDecodedBody()
185
    {
186
        return $this->decodedBody;
187
    }
188
189
    /**
190
     * @return mixed
191
     */
192
    public function getMailxpertNode()
193
    {
194
        return ModelFactory::getNode($this->getRequest()->getEndpoint(), $this->getDecodedBody());
195
    }
196
197
    /**
198
     * @return MailxpertSDKException
199
     */
200
    public function getThrownException()
201
    {
202
        return $this->thrownException;
203
    }
204
205
    /**
206
     * @return bool
207
     */
208
    public function isError()
209
    {
210
        return isset($this->decodedBody['error']);
211
    }
212
213
    /**
214
     * Make exception
215
     */
216
    public function makeException()
217
    {
218
        $this->thrownException = new MailxpertSDKException($this->decodedBody['error']);
219
    }
220
}
221