|
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
|
|
|
protected $instapushClient; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \Psr\Http\Message\RequestInterface */ |
|
28
|
|
|
protected $request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \Fnayou\InstapushPHP\InstapushClient $instapushClient |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(InstapushClient $instapushClient) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->instapushClient = $instapushClient; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return \Fnayou\InstapushPHP\InstapushClient |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getInstapushClient() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->instapushClient; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \Fnayou\InstapushPHP\InstapushClient $instapushClient |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function setInstapushClient(InstapushClient $instapushClient) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->instapushClient = $instapushClient; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getRequest() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->request; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function setRequest(RequestInterface $request) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->request = $request; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
72
|
|
|
* @param string $class |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function transformResponse(ResponseInterface $response, $class) |
|
77
|
|
|
{ |
|
78
|
|
|
// return \GuzzleHttp\Psr7\Response |
|
79
|
|
|
if (!$this->getInstapushClient()->getTransformer()) { |
|
80
|
|
|
return $response; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// handle exception |
|
84
|
|
|
if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) { |
|
85
|
|
|
return $this->handleException($response); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// transform response according to class |
|
89
|
|
|
return $this->getInstapushClient()->getTransformer()->transform($response, $class); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @param array $parameters |
|
95
|
|
|
* @param array $headers |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function doGet(string $path, array $parameters = [], array $headers = []) |
|
100
|
|
|
{ |
|
101
|
|
|
if (0 < \count($parameters)) { |
|
102
|
|
|
$path .= '?'.\http_build_query($parameters); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$request = $this |
|
106
|
|
|
->getInstapushClient() |
|
107
|
|
|
->getRequestFactory() |
|
108
|
|
|
->createRequest('GET', $path, $headers); |
|
109
|
|
|
|
|
110
|
|
|
$this->setRequest($request); |
|
111
|
|
|
|
|
112
|
|
|
return $this->instapushClient->getHttpClient()->sendRequest($request); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function handleException(ResponseInterface $response) |
|
121
|
|
|
{ |
|
122
|
|
|
if (true !== $response->hasHeader('Content-Type') |
|
123
|
|
|
|| 'application/json' !== $response->getHeaderLine('Content-Type') |
|
124
|
|
|
) { |
|
125
|
|
|
throw new ApiException( |
|
126
|
|
|
\sprintf( |
|
127
|
|
|
'Waiting for json response but %s given', |
|
128
|
|
|
$response->getHeaderLine('Content-Type') |
|
129
|
|
|
), |
|
130
|
|
|
$this->getRequest(), |
|
131
|
|
|
$response |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (false === $this->getInstapushClient()->isHandleException()) { |
|
136
|
|
|
return $this->getInstapushClient()->getTransformer()->transform($response, ApiError::class); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$content = \json_decode($response->getBody()->getContents(), true); |
|
140
|
|
|
|
|
141
|
|
|
if (true === isset($content['msg'])) { |
|
142
|
|
|
$message = $content['msg']; |
|
143
|
|
|
} else { |
|
144
|
|
|
$message = 'An unexpected/unknown error occurred : '.$content; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
throw new ApiException($message, $this->getRequest(), $response); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|