1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the fnayou/instapush-php project. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017. Aymen FNAYOU <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fnayou\InstapushPHP\Api; |
12
|
|
|
|
13
|
|
|
use Fnayou\InstapushPHP\Exception\ApiException; |
14
|
|
|
use Fnayou\InstapushPHP\InstapushClient; |
15
|
|
|
use Fnayou\InstapushPHP\Model\ApiError; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class AbstractApi. |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractApi |
23
|
|
|
{ |
24
|
|
|
/** @var \Fnayou\InstapushPHP\InstapushClient */ |
25
|
|
|
private $instapushClient; |
26
|
|
|
|
27
|
|
|
/** @var \Psr\Http\Message\RequestInterface */ |
28
|
|
|
private $request; |
29
|
|
|
|
30
|
|
|
/** @var \Psr\Http\Message\ResponseInterface */ |
31
|
|
|
private $response; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \Fnayou\InstapushPHP\InstapushClient $instapushClient |
35
|
|
|
*/ |
36
|
|
|
public function __construct(InstapushClient $instapushClient) |
37
|
|
|
{ |
38
|
|
|
$this->instapushClient = $instapushClient; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \Fnayou\InstapushPHP\InstapushClient |
43
|
|
|
*/ |
44
|
|
|
public function getInstapushClient() |
45
|
|
|
{ |
46
|
|
|
return $this->instapushClient; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \Psr\Http\Message\RequestInterface |
51
|
|
|
*/ |
52
|
|
|
public function getRequest() |
53
|
|
|
{ |
54
|
|
|
return $this->request; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
59
|
|
|
*/ |
60
|
|
|
public function getResponse() |
61
|
|
|
{ |
62
|
|
|
return $this->response; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Fnayou\InstapushPHP\InstapushClient $instapushClient |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
protected function setInstapushClient(InstapushClient $instapushClient) |
71
|
|
|
{ |
72
|
|
|
$this->instapushClient = $instapushClient; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
protected function setRequest(RequestInterface $request) |
83
|
|
|
{ |
84
|
|
|
$this->request = $request; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
protected function setResponse(ResponseInterface $response) |
95
|
|
|
{ |
96
|
|
|
$this->response = $response; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $path |
103
|
|
|
* @param array $parameters |
104
|
|
|
* @param array $headers |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
protected function doGet(string $path, array $parameters = [], array $headers = []) |
109
|
|
|
{ |
110
|
|
|
if (0 < \count($parameters)) { |
111
|
|
|
$path .= '?'.\http_build_query($parameters); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$request = $this |
115
|
|
|
->getInstapushClient() |
116
|
|
|
->getRequestFactory() |
117
|
|
|
->createRequest('GET', $path, $headers); |
118
|
|
|
|
119
|
|
|
$this->setRequest($request); |
120
|
|
|
|
121
|
|
|
$response = $this |
122
|
|
|
->getInstapushClient() |
123
|
|
|
->getHttpClient() |
124
|
|
|
->sendRequest($request); |
125
|
|
|
|
126
|
|
|
$this->setResponse($response); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $path |
133
|
|
|
* @param array $parameters |
134
|
|
|
* @param array $headers |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
protected function doPost(string $path, array $parameters = [], array $headers = []) |
139
|
|
|
{ |
140
|
|
|
$body = \json_encode($parameters); |
141
|
|
|
|
142
|
|
|
$request = $this |
143
|
|
|
->getInstapushClient() |
144
|
|
|
->getRequestFactory() |
145
|
|
|
->createRequest('POST', $path, $headers, $body); |
146
|
|
|
|
147
|
|
|
$this->setRequest($request); |
148
|
|
|
|
149
|
|
|
$response = $this |
150
|
|
|
->getInstapushClient() |
151
|
|
|
->getHttpClient() |
152
|
|
|
->sendRequest($request); |
153
|
|
|
|
154
|
|
|
$this->setResponse($response); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $class |
161
|
|
|
* |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
protected function transformResponse(string $class = null) |
165
|
|
|
{ |
166
|
|
|
if (200 !== $this->getResponse()->getStatusCode() |
167
|
|
|
&& 201 !== $this->getResponse()->getStatusCode() |
168
|
|
|
) { |
169
|
|
|
return $this->handleException(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (null === $this->getInstapushClient()->getTransformer() || null === $class) { |
173
|
|
|
return \json_decode($this->getResponse()->getBody()->getContents(), true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->getInstapushClient()->getTransformer()->transform($this->getResponse(), $class); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @throws \Fnayou\InstapushPHP\Exception\ApiException |
181
|
|
|
* |
182
|
|
|
* @return \Fnayou\InstapushPHP\Model\ApiError |
183
|
|
|
*/ |
184
|
|
|
protected function handleException() |
185
|
|
|
{ |
186
|
|
|
$this->handleNotJsonException($this->getResponse()); |
187
|
|
|
|
188
|
|
|
if (false === $this->getInstapushClient()->isHandleException()) { |
189
|
|
|
return $this |
190
|
|
|
->getInstapushClient() |
191
|
|
|
->getTransformer() |
192
|
|
|
->transform($this->getResponse(), ApiError::class); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$content = \json_decode($this->getResponse()->getBody()->__toString(), true); |
196
|
|
|
|
197
|
|
|
$message = true === isset($content['msg']) ? $content['msg'] : 'An unexpected error occurred : '.$content; |
198
|
|
|
|
199
|
|
|
throw new ApiException($message, $this->getRequest(), $this->getResponse()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param ResponseInterface $response |
204
|
|
|
* |
205
|
|
|
* @throws \Fnayou\InstapushPHP\Exception\ApiException |
206
|
|
|
*/ |
207
|
|
|
protected function handleNotJsonException(ResponseInterface $response) |
208
|
|
|
{ |
209
|
|
|
if (true !== $response->hasHeader('Content-Type') |
210
|
|
|
|| 'application/json' !== $response->getHeaderLine('Content-Type') |
211
|
|
|
) { |
212
|
|
|
throw new ApiException( |
213
|
|
|
\sprintf( |
214
|
|
|
'Waiting for json response but %s given', |
215
|
|
|
$response->getHeaderLine('Content-Type') |
216
|
|
|
), |
217
|
|
|
$this->getRequest(), |
218
|
|
|
$response |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|