1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient\Http; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Exception\ServerException; |
8
|
|
|
use Mechpave\ImgurClient\Exception\AuthenticationException; |
9
|
|
|
use Mechpave\ImgurClient\Exception\InvalidParameterException; |
10
|
|
|
use Mechpave\ImgurClient\Exception\NotFoundException; |
11
|
|
|
use Mechpave\ImgurClient\Exception\RateLimitException; |
12
|
|
|
use Mechpave\ImgurClient\Exception\ServerErrorException; |
13
|
|
|
use Mechpave\ImgurClient\ImgurClient; |
14
|
|
|
use Mechpave\ImgurClient\Model\EndpointModel; |
15
|
|
|
use Mechpave\ImgurClient\Model\StatusCodeModel; |
16
|
|
|
|
17
|
|
|
class HttpClient implements HttpClientInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Imgur client |
21
|
|
|
* |
22
|
|
|
* @var ImgurClient |
23
|
|
|
*/ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* HTTP client object |
28
|
|
|
* |
29
|
|
|
* @var GuzzleClient |
30
|
|
|
*/ |
31
|
|
|
protected $httpClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Client constructor. |
35
|
|
|
* @param ImgurClient $client |
36
|
|
|
*/ |
37
|
8 |
|
public function __construct(ImgurClient $client) |
38
|
|
|
{ |
39
|
8 |
|
$this->client = $client; |
40
|
8 |
|
$this->initHttpClient(); |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function get($uri, array $queryParams = null) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$options = []; |
49
|
|
|
|
50
|
|
|
if (!empty($queryParams)) { |
51
|
|
|
$options['query'] = $queryParams; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$response = $this->makeRequest('GET', $uri, $options); |
55
|
|
|
|
56
|
|
|
return $response; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function post($uri, array $postData = null) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$options = []; |
65
|
|
|
|
66
|
|
|
if (!empty($postData)) { |
67
|
|
|
$options['form_params'] = $postData; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$response = $this->makeRequest('POST', $uri, $options); |
71
|
|
|
|
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function delete($uri) |
79
|
|
|
{ |
80
|
|
|
$response = $this->makeRequest('DELETE', $uri); |
81
|
|
|
|
82
|
|
|
return $response; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Makes http request and handles Imgur errors |
87
|
|
|
* |
88
|
|
|
* @param string $method |
89
|
|
|
* @param string $uri |
90
|
|
|
* @param array $options |
91
|
|
|
* @return ImgurResponseInterface |
92
|
|
|
* @throws AuthenticationException |
93
|
|
|
* @throws InvalidParameterException |
94
|
|
|
* @throws NotFoundException |
95
|
|
|
* @throws RateLimitException |
96
|
|
|
* @throws ServerErrorException |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
6 |
|
protected function makeRequest($method, $uri = null, $options = []) |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
6 |
|
$response = $this->httpClient->request($method, $uri, $options); |
103
|
5 |
|
} catch (ClientException $e) { |
104
|
|
|
//4** status codes |
105
|
4 |
|
$statusCode = $e->getResponse()->getStatusCode(); |
106
|
4 |
|
$body = json_decode($e->getResponse()->getBody()->getContents(), true); |
107
|
|
|
|
108
|
|
|
switch ($statusCode) { |
109
|
4 |
|
case StatusCodeModel::PARAMETER_MISSING: |
110
|
1 |
|
throw new InvalidParameterException($body['data']['error'], $statusCode); |
111
|
|
|
break; |
|
|
|
|
112
|
3 |
|
case StatusCodeModel::AUTHENTICATION_REQUIRED: |
113
|
2 |
|
case StatusCodeModel::FORBIDDEN: |
114
|
1 |
|
throw new AuthenticationException($body['data']['error'], $statusCode); |
115
|
|
|
break; |
|
|
|
|
116
|
2 |
|
case StatusCodeModel::RESOURCE_NOT_FOUND: |
117
|
1 |
|
throw new NotFoundException($body['data']['error'], $statusCode); |
118
|
|
|
break; |
|
|
|
|
119
|
1 |
|
case StatusCodeModel::RATE_LIMITING: |
120
|
1 |
|
throw new RateLimitException($body['data']['error'], $statusCode); |
121
|
|
|
break; |
|
|
|
|
122
|
|
|
default: |
123
|
|
|
throw $e; |
124
|
|
|
break; |
|
|
|
|
125
|
|
|
} |
126
|
1 |
|
} catch (ServerException $e) { |
127
|
|
|
//5** status codes |
128
|
1 |
|
$statusCode = $e->getResponse()->getStatusCode(); |
129
|
1 |
|
throw new ServerErrorException('Internal Imgur server error', $statusCode); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
//unknown error |
132
|
|
|
throw $e; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$imgurResponse = new ImgurResponse($response); |
136
|
|
|
|
137
|
1 |
|
return $imgurResponse; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Initializes HTTP client and sets base URI |
142
|
|
|
*/ |
143
|
8 |
|
protected function initHttpClient() |
144
|
|
|
{ |
145
|
8 |
|
$baseUri = EndpointModel::STANDARD; |
146
|
|
|
|
147
|
8 |
|
if ($this->client->getMashapeKey()) { |
148
|
1 |
|
$baseUri = EndpointModel::COMMERCIAL; |
149
|
|
|
} |
150
|
|
|
|
151
|
8 |
|
$headers = $this->prepareAuthenticationHeaders(); |
152
|
|
|
|
153
|
8 |
|
$this->httpClient = new GuzzleClient([ |
154
|
8 |
|
'base_uri' => $baseUri, |
155
|
8 |
|
'headers' => $headers, |
156
|
|
|
]); |
157
|
8 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Prepare Authentication headers required by Imgur and/or Mashape |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
8 |
|
protected function prepareAuthenticationHeaders() |
165
|
|
|
{ |
166
|
8 |
|
$headers = []; |
167
|
|
|
|
168
|
8 |
|
$headers['Authorization'] = 'Client-ID ' . $this->client->getClientId(); |
169
|
8 |
|
if($this->client->getToken() && $this->client->getToken()->getAccessToken()) { |
170
|
1 |
|
$headers['Authorization'] = 'Bearer ' . $this->client->getToken()->getAccessToken(); |
171
|
|
|
} |
172
|
|
|
|
173
|
8 |
|
if($this->client->getMashapeKey()) { |
174
|
1 |
|
$headers['X-Mashape-Key'] = $this->client->getMashapeKey(); |
175
|
|
|
} |
176
|
|
|
|
177
|
8 |
|
return $headers; |
178
|
|
|
} |
179
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.