1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pexels-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\Pexels\Provider; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Exception; |
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use WBW\Library\Pexels\API\PaginateResponseInterface; |
19
|
|
|
use WBW\Library\Pexels\API\SubstituteRequestInterface; |
20
|
|
|
use WBW\Library\Pexels\Exception\APIException; |
21
|
|
|
use WBW\Library\Pexels\Model\AbstractRequest; |
22
|
|
|
use WBW\Library\Pexels\Traits\RateLimitTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Abstract provider. |
26
|
|
|
* |
27
|
|
|
* @author webeweb <https://github.com/webeweb/> |
28
|
|
|
* @package WBW\Library\Pexels\Provider |
29
|
|
|
* @abstract |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractProvider { |
32
|
|
|
|
33
|
|
|
use RateLimitTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Endpoint path. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
const ENDPOINT_PATH = "https://api.pexels.com"; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Authorization. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $authorization; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Debug. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private $debug; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
*/ |
59
|
|
|
public function __construct() { |
60
|
|
|
$this->setDebug(false); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Build the configuration. |
65
|
|
|
* |
66
|
|
|
* @return array Returns the configuration. |
67
|
|
|
*/ |
68
|
|
|
private function buildConfiguration() { |
69
|
|
|
return [ |
70
|
|
|
"debug" => $this->getDebug(), |
71
|
|
|
"headers" => [ |
72
|
|
|
"Accept" => "application/json", |
73
|
|
|
"User-Agent" => "webeweb/pexels-library", |
74
|
|
|
"Authorization" => $this->getAuthorization(), |
75
|
|
|
], |
76
|
|
|
"synchronous" => true, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Build a resource path. |
82
|
|
|
* |
83
|
|
|
* @param AbstractRequest $request The request. |
84
|
|
|
* @return string Returns the resource path. |
85
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
86
|
|
|
*/ |
87
|
|
|
private function buildResourcePath(AbstractRequest $request) { |
88
|
|
|
|
89
|
|
|
if (false === ($request instanceof SubstituteRequestInterface)) { |
90
|
|
|
return $request->getResourcePath(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (null === $request->getSubstituteValue()) { |
94
|
|
|
throw new InvalidArgumentException(sprintf("The substitute value %s is missing", $request->getSubstituteName())); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return str_replace($request->getSubstituteName(), $request->getSubstituteValue(), $request->getResourcePath()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Call the API. |
102
|
|
|
* |
103
|
|
|
* @param string $uri The URI. |
104
|
|
|
* @param array $queryData The query data. |
105
|
|
|
* @return string Returns the raw response. |
106
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
107
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
108
|
|
|
*/ |
109
|
|
|
private function callAPI($uri, array $queryData) { |
110
|
|
|
|
111
|
|
|
if (null === $this->getAuthorization()) { |
112
|
|
|
throw new InvalidArgumentException("The mandatory parameter \"authorization\" is missing"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
|
117
|
|
|
$client = new Client($this->buildConfiguration()); |
118
|
|
|
|
119
|
|
|
$options = 0 < count($queryData) ? ["query" => $queryData] : []; |
120
|
|
|
|
121
|
|
|
$response = $client->request("GET", $uri, $options); |
122
|
|
|
|
123
|
|
|
$this->setLimit(intval($response->getHeaderLine("X-Ratelimit-Limit"))); |
124
|
|
|
$this->setRemaining(intval($response->getHeaderLine("X-Ratelimit-Remaining"))); |
125
|
|
|
$this->setReset(new DateTime("@" . $response->getHeaderLine("X-Ratelimit-Reset"))); |
126
|
|
|
|
127
|
|
|
return $response->getBody()->getContents(); |
128
|
|
|
} catch (Exception $ex) { |
129
|
|
|
|
130
|
|
|
throw new APIException("Call Pexels API failed", $ex); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Call the API. |
136
|
|
|
* |
137
|
|
|
* @param AbstractRequest $request The request. |
138
|
|
|
* @param array $queryData The query data. |
139
|
|
|
* @return string Returns the raw response. |
140
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
141
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
142
|
|
|
*/ |
143
|
|
|
protected function callAPIWithRequest(AbstractRequest $request, array $queryData) { |
144
|
|
|
|
145
|
|
|
try { |
146
|
|
|
|
147
|
|
|
$uri = self::ENDPOINT_PATH . $this->buildResourcePath($request); |
148
|
|
|
|
149
|
|
|
return $this->callAPI($uri, $queryData); |
150
|
|
|
} catch (InvalidArgumentException $ex) { |
151
|
|
|
|
152
|
|
|
throw $ex; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Call the API. |
158
|
|
|
* |
159
|
|
|
* @param PaginateResponseInterface $response The request. |
160
|
|
|
* @param bool $nextPage Next page ?. |
161
|
|
|
* @return string Returns the raw response. |
162
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
163
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
164
|
|
|
*/ |
165
|
|
|
protected function callAPIWithResponse(PaginateResponseInterface $response, $nextPage) { |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
|
169
|
|
|
$uri = false === $nextPage ? $response->getPrevPage() : $response->getNextPage(); |
170
|
|
|
if (null === $uri) { |
171
|
|
|
return ""; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this->callAPI($uri, []); |
175
|
|
|
} catch (InvalidArgumentException $ex) { |
176
|
|
|
|
177
|
|
|
throw $ex; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the authorization. |
183
|
|
|
* |
184
|
|
|
* @return string Returns the authorization. |
185
|
|
|
*/ |
186
|
|
|
public function getAuthorization() { |
187
|
|
|
return $this->authorization; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the debug. |
192
|
|
|
* |
193
|
|
|
* @return bool Returns the debug. |
194
|
|
|
*/ |
195
|
|
|
public function getDebug() { |
196
|
|
|
return $this->debug; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the authorization. |
201
|
|
|
* |
202
|
|
|
* @param string $authorization The authorization. |
203
|
|
|
* @return AbstractProvider Returns this provider. |
204
|
|
|
*/ |
205
|
|
|
public function setAuthorization($authorization) { |
206
|
|
|
$this->authorization = $authorization; |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the debug. |
212
|
|
|
* |
213
|
|
|
* @param bool $debug The debug. |
214
|
|
|
* @return AbstractProvider Returns this provider. |
215
|
|
|
*/ |
216
|
|
|
public function setDebug($debug) { |
217
|
|
|
$this->debug = $debug; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|